How to Set Value of a Structure as Cell Array?
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2017-5-12
DLG.standard.Z1P{1} = C;
3 个评论
Monica Nelson
2020-9-29
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
2020-9-29
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.
更多回答(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!