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

 채택된 답변

Guillaume
Guillaume 2018년 7월 10일
편집: Guillaume 2018년 7월 10일

1 개 추천

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.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2018년 7월 10일

편집:

2018년 7월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by