MATLAB Answers

How to Set Value of a Structure as Cell Array?

14 views (last 30 days)
I have a structure, DLG.standard.Z1P and I have a cell array of data, C (2740x360). I would like to put this cell array within the structure DLG.standard.Z1P. I have three of these cell arrays that correspond to Z1P, and I would like to assign them such that the length of DLG.standard.Z1P is three, where Z1P has three cell arrays, each of size 2740x360. Any ideas on how to go about this?
I tried something like
DLG.standard.Z1P(1)=C
but that does not work.
Doing
DLG.standard.Z1P(1)=1;
DLG.standard.Z1P(2)=1;
does what I want it to, however, instead of 1, I want the whole cell array, C, in that location.

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 12 May 2017
DLG.standard.Z1P{1} = C;

  3 Comments

Ibro Tutic
Ibro Tutic on 12 May 2017
Perfect, my issue was I was trying to assign a cell array to a non-cell array. Thanks
Monica Nelson
Monica Nelson on 29 Sep 2020
Can you do a similar thing for just one cell array and have it directly stored without sublevels?
I am trying to store data and identifiers in a structure and want to have the identifier (which the cell array 'expocode') at the same level as the data.
I have
D = struct('lon',longitude,'lat', latitude, 'expocode', expocode);
but D.expocode only gives the first value in the cell.
When I instead try the solution suggested above, ie.
D = struct('lon',longitude,'lat', latitude);
D.expocode{1}=expocode;
I get that D.expocode is a 1x1 cell array, rather than the contents of the cell array itself. How can I write this so that D.expocode is the cell array contents itself?
Thank you!
Walter Roberson
Walter Roberson on 29 Sep 2020
Compare:
D = struct('lon',longitude,'lat', latitude, 'expocode', {expocode});
Provided that longitude and latitude are not cell arrays, then the result would be that D would be a scalar structure with fields lon and lat and expocode and the expocode field would be the full cell array expocode.
To:
D = struct('lon',longitude,'lat', latitude, 'expocode', expocode);
Provided that longitude and latitude are not cell arrays, then the result would be that D would be a non-scalar structure the same size as the cell array expocode, and D(K).lon would be a copy of longitude and D(K).lat would be a copy of the array latitude, and D(K).expocode would be a copy of expocode{K}
To:
D = struct('lon',longitude,'lat', latitude);
D.expocode = expocode;
Provided that longitude and latitude are not cell arrays, then the result would be that D would be a scalar structure with fields lon and lat and expocode and the expocode field would be the full cell array expocode. The result would be the same as the first syntax I showed.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by