How to read the details of a '1×1 struct' Cell Array?
117 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a variable called L1, which is a '1×1 struct' Cell Array. I want to extract the details of this variable, but I tried different functions including fieldnames(), fields() and struct2table(), and all of them doesn't work.
I put the variable inside a small .mat file as attached (L1.mat). How can I access the details of the variable? Thanks!
0 个评论
采纳的回答
Stephen23
2019-3-17
编辑:Stephen23
2019-3-17
Both cell arrays and structure arrays are kinds of container arrays, i.e. they can contain other arrays. In your case you actually have a scalar cell array containing a scalar structure. This is easy to check:
>> class(L1) % tells us L1 is a cell array
ans = cell
>> size(L1) % tells us L1 is scalar
ans =
1 1
Now that we know that L1 is a cell array, the obvious thing to do is to find out what it contains, which we can do easily using basic cell array indexing:
>> S = L1{1}; % allocate the content of that cell to a variable S.
>> class(S) % tells us S is a structure
ans = struct
>> size(S) % tells us S is scalar
ans =
1 1
Now that we know that scalar cell array L1 contains a scalar structure, it is easy to access the structure's fields or do any other structure operations with it:
>> C = fieldnames(L1{1}); % the fieldnames of the structure inside the cell array,
>> C = fieldnames(S); % or equivalently the fieldnames of the structure by itself.
>> C{:}
ans = name
ans = type
ans = weights
ans = size
ans = pad
ans = stride
ans = precious
ans = dilate
ans = opts
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!