MATLAB Answers

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

조회 수: 261(최근 30일)
SC
SC 2019년 3월 17일
편집: Stephen 2019년 3월 17일
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!

채택된 답변

Stephen
Stephen 2019년 3월 17일
편집: Stephen 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개)

Community Treasure Hunt

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

Start Hunting!

Translated by