I have problem to access each field of struct in MATLAB. I tried to convert it to Cell however, it give me error :( How can I access each field with 2 loops ? I have wrote
You can access a structure dynamically with s.(fname)
where fname
is char variable. Note the ( )
around fname
.
An example will clarify:
% Example structure
s.A = 10;
s.B = 20;
% Retrieve fieldnames
fnames = fieldnames(s);
% Loop with numeric index
for ii = 1:numel(fnames)
s.(fnames{ii})
end
% ...or loop by fieldname directly
for f = fnames'
s.(f{:})
end