Tab completion of struct within cell

조회 수: 3 (최근 30일)
Anna
Anna 2019년 3월 18일
댓글: Anna 2019년 3월 25일
Hello together,
I am working on a large object-oriented database where it happens that I store structs or instances of classes within a cell. However, I experience that the autocomplete or tab completion function of Matlab does not work anymore after the cell. So, for example I define a struct "structTest":
structTest.a = 2;
structTest.b = 4;
The autocompletion works fine:
StructTestTabCompletion.png
Now I save this struct within the cell "cellTest":
cellTest = cell(2,1);
cellTest{1,1} = structTest;
Unfortunately, the autocompletion does not work anymore:
CellTestTabCompletion.png
Similarly, the problem occurs when I save instances of classes within a cell.
After reading about .json files for tab completion of functions and overriding the properties(obj) function in the class definition with fieldnames(obj), I still don't have a clue on how to deal with this. Especially since fieldnames(cellTest{1,1}) works just fine:
>> fieldnames(cellTest{1,1})
ans =
2×1 cell array
{'a'}
{'b'}
Any suggestions on this? It would be of great advantage, as users sometimes have to navigate quite deeply into the database.
Thank you very much for your help,
Anna

채택된 답변

Guillaume
Guillaume 2019년 3월 18일
The reason why tab completion doesn't work for the content of a cell array can easily be explained: matlab does not look into the cell array to see what type of data is stored there.
Now you could argue that maybe it should but maybe it shouldn't. After all, cell arrays are used to store heterogeneous data, so most likely while it may be a structure in there now, maybe it will be a double next time. cell content tend to change type more often than variables do. And if all the cells are always a structure, why aren't you using a structure array instead? Tab completion will work without problems on a structure array.
In any case, the current version of matlab does not look inside the cell array, so there's no work around.
  댓글 수: 3
Guillaume
Guillaume 2019년 3월 19일
By default, class instances are arrays, so you don't need a cell array for that (unless you've coded the class in a way that completely breaks if the instances are not scalar, or you've specifically disallowed array creation).
Even if you want to store polymorphic objects, if they all inherit from the same base class, you can all store them in a plain array, as long as the base class inherits from matlab.mixin.Heterogeneous.
But yes, sometimes you have to use a cell array and you won't get tab completion.
Anna
Anna 2019년 3월 19일
"By default, class instances are arrays, so you don't need a cell array for that (unless you've coded the class in a way that completely breaks if the instances are not scalar, or you've specifically disallowed array creation). "
I didn't know that - just tried it and it works fantastically! Thanks for the hint!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ken Atwell
Ken Atwell 2019년 3월 23일
If you can try the just-released R2019a, MATLAB will now tab completion in this situation.
  댓글 수: 1
Anna
Anna 2019년 3월 25일
Oh really? Great!! I'll propably wait until the release of 2019b, but then will definitively upgrade! Thank you very much for this information!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Types에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by