assign cell arrays to struct

43 Ansichten (letzte 30 Tage)
René Lampert
René Lampert am 28 Mai 2022
Beantwortet: Stephen23 am 28 Mai 2022
Dear all,
is it somehow possible to assign cell arrays to the "field" and "value" variables within a struct, like test=struct(field,value) where "field" and "value" are cell arrays ?
Thanks

Akzeptierte Antwort

Stephen23
Stephen23 am 28 Mai 2022
You could use CELL2STRUCT :
fnm = {'hello','world'};
val = {[1,NaN],[1,4,9]};
S2 = cell2struct(val,fnm,2)
S2 = struct with fields:
hello: [1 NaN] world: [1 4 9]
or a comma-separated list:
tmp = [fnm;val]; % the orientation is important!
S1 = struct(tmp{:})
S1 = struct with fields:
hello: [1 NaN] world: [1 4 9]

Weitere Antworten (1)

Image Analyst
Image Analyst am 28 Mai 2022
ca = {1, 'abc', rand(1,4)}
ca = 1×3 cell array
{[1]} {'abc'} {[0.3876 0.4122 0.2440 0.2197]}
test.myField = ca
test = struct with fields:
myField: {[1] 'abc' [0.3876 0.4122 0.2440 0.2197]}

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by