Tab completion of struct within cell
4 次查看(过去 30 天)
显示 更早的评论
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:
Now I save this struct within the cell "cellTest":
cellTest = cell(2,1);
cellTest{1,1} = structTest;
Unfortunately, the autocompletion does not work anymore:
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
0 个评论
采纳的回答
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
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.
更多回答(1 个)
Ken Atwell
2019-3-23
If you can try the just-released R2019a, MATLAB will now tab completion in this situation.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!