For loop iteration help

조회 수: 2 (최근 30일)
ajk1
ajk1 2016년 8월 3일
댓글: ajk1 2016년 8월 4일
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.
  댓글 수: 14
ajk1
ajk1 2016년 8월 4일
Apologies, this will be my last question here.
I have
for k=51:3:69
Is there a way of removing empty structure fields
I have datasets in S(51).val, S(54).val, ... S(69.val). But not in S(1).val, S(52).val.
I have thought to use number sequences and change 51, 54, 57,...69 to 1, 2, 3, ...7. But later on it will cause confusion with the next part of my script.
ajk1
ajk1 2016년 8월 4일
Okay, I have solved this. Thanks again!

댓글을 달려면 로그인하십시오.

채택된 답변

Geoff Hayes
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
ajk1
ajk1 2016년 8월 4일
This works for both the image and the corresponding dataset. For the datasets I changed k=1:numel(data). Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
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
  댓글 수: 2
ajk1
ajk1 2016년 8월 4일
Thank you, I am also using this method to store the corresponding image dataset (375x91x223). When I use this method I obtain the error 'Conversion to double from cell is not possible.'
ajk1
ajk1 2016년 8월 4일
It's working now! Thanks again.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by