Matlab - for loop over cell array "foreach"-like syntax loops only over the first element -
this question has answer here:
i thought if write
for x = cell_array ... end
then loop run on elements of cell_array
, in following case doesn't:
>> tags tags = 'dset3' 'dset4' 'cpl1' >> class(tags) ans = cell >> t = tags tmp = t{:} %no semicolon: i.e. print it. end tmp = dset3
so works first element.
what's problem?
according the documentation, for x = cell_array
iterate on columns of cell array.
the reason confusion in question how {:}
expansion behaves:
>> = {3;4} = [3] [4] >> b = a{:} b = 3
in above, a{:}
akin typing in comma-separated list elements elements of cell array a
. except not quite! if write such list explicitly, get:
>> c = 3,4 c = 3 ans = 4
somehow, >> b = a{:}
, other elements of a
discarded silently, if e.g. a = {1 2; 3 4}
.
however, in other contexts, a{:}
expand full comma-separated list:
>> extra_args = {'*-'; 'linewidth'; 30}; >> plot(1:2, extra_args{:}) >> extra_args = {}; >> plot(1:2, extra_args{:})
this it's intended do.
Comments
Post a Comment