For loop iteration help
1 次查看(过去 30 天)
显示 更早的评论
Hi, in my code I have
for k=1:20
% code executed with output 'image'
data.(['val' num2str(k)]) = image; % save the dataset in 'data'. Can access data.val1, ... data.val20
end
I want to write another for loop to go through 'data.val1', going to 'data.val20' to execute another script I have written i.e. for k=1:20 data.val...(value of k for iteration). Help doing this will be appreciated. Thanks.
采纳的回答
Geoff Hayes
2016-8-4
ajk1 - rather dynamically creating field names for your struct (which is prone to errors), why not just create a (perhaps) cell array to store all of your 20 images in? You can easily iterate over the cell array and extract the image data in your other loop
data = cell(20,1);
for k=1:20
% do something
data{k} = myImage; % don't use image which is the name of a built-in MATLAB function
end
Then, in your other code you would do
for k=1:length(data)
myImage = data{k};
% do something
end
Try the above and see what happens!
更多回答(1 个)
Image Analyst
2016-8-4
Why not have your other processing or loop be a loop inside this one, and not bother about saving all your sub-images in an extra variable (a structure array or cell array)? From what you've told us so far, there is no need to save subimages in an array. Just save them in a single variable and overwrite it each time.
for k = 1 : 20
% Extract a sub-image.
subImage = myImage(......
results(k) = ProcessSubImage(subImage); % Now process it in a function.
end
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!