Clear Filters
Clear Filters

Structure to matrix

3 views (last 30 days)
Anna
Anna on 24 Apr 2012
Hello,
I am trying to convert a structure with 40 fields into a single 3D matrix. I tried using struct2cell but got an error ??? Undefined function or method 'struct2cell' for input arguments of type 'double'.
I then tried creating a loop (below) but it writes the first field 40 times instead of writing all 40 fields once.. and I'm not sure how to fix it. The structure (runoff) has 40 fields, each with dimensions 12x360x720. I want to create a matrix (runoff_all) that has all the data in the structure and dimensions 480x360x720. How can I achieve this?
varnames=fieldnames(runoff);
runoff_all=zeros(480,360,720);
for k=1:length(varnames)
for i=1:12:469
runoff_all(i:i+11,:,:)=runoff.(varnames{k})(1:12,:,:);
end
end
  1 Comment
Walter Roberson
Walter Roberson on 24 Apr 2012
Please show the call you tried for struct2cell()

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 24 Apr 2012
'struct2cell' - built-in function of MATLAB
>> which struct2cell
about loop for .. end
varnames=fieldnames(runoff);
runoff_all=zeros(480,360,720);
for k=1:length(varnames)
frunoff_all(12*k + (-11:0),:,:)=runoff.(varnames{k});
end

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!