MATLAB Answers

SC
0

How to read the details of a '1×1 struct' Cell Array?

Asked by SC
on 17 Mar 2019 at 0:18
Latest activity Edited by Stephen Cobeldick on 17 Mar 2019 at 12:45
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 Comments

Sign in to comment.

1 Answer

Answer by Stephen Cobeldick on 17 Mar 2019 at 12:43
Edited by Stephen Cobeldick on 17 Mar 2019 at 12:45
 Accepted Answer

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 Comments

Sign in to comment.