Lets say I have a map
results = containers.Map;
and I add some things to my map such as
results('a') = struct('hello', 1, 'world', 2);
results('b') = struct('foo', 3, 'bar', 4);
how would I loop through my results map to get the stucts back.
Example that works:
for k = results.keys
key = k{1}; % Since k is a 1x1 cell
results(key)
end
Is there a way to have k "unpack" the cell in the for statement, something like this is what I am looking for
for {k} = results.keys % Does not work, can't unpack a cell like this {k}, has to be k{1}
results(k)
end
any suggestions on how to write this nicely?
my current solution is
for k = results.keys
results(k{1}) %Seems a bit hard to read.
end

 Akzeptierte Antwort

Guillaume
Guillaume am 10 Jul. 2018
Bearbeitet: Guillaume am 10 Jul. 2018
Yes, it would be nice if matlab had something like a foreach. However, it does not and the only option you're left with is
result(k{1})
indeed.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-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