The usual way to iterate through a struct data type in MATLAB is using the fieldnames()
function as done in:
mystruct = struct('a',3,'b',5,'c',9);
fields = fieldnames(mystruct);
for i=1:numel(fields)
mystruct.(fields{i});
end
Unfortunately, this always generates cell data types, and I would like to use this kind of iteration for a Matlab Function Block in SIMULINK that does not allow cell data types for code generation reasons.
Is there a way to iterate through a structure without making use of cell data types at the same time?
In Octave there is a neat way of that explained in https://www.gnu.org/software/octave/doc/interpreter/Looping-Over-Structure-Elements.html
for [val, key] = mystruct
# do something esp. with 'key'
end
Does anybody know a similar way in MATLAB?
structfun
can help. – Obazamystruct
, that is, replace the values 3, 5, 9 with [10, 11, 12], to stay with the example. I don't know yet how to do this with astructfun
statement under this circumstances. Any hints? Could it be a first step to transform the struct? Does a simple function (without {} occuring!) exists for transformation of a 1x1 struct to that 1x3 form:struct('key',{'a','b','c'},'val',{3,5,9})
? – Gahnitefieldnames
was added along with some other cell array support for code generation. – Clareta