For loop iteration help

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 个评论

Rather than awkward and messy dynamic fieldnames, just use a non-scalar structure. Much simpler:
>> S(1).data = 1:3;
>> S(2).data = 3:5;
>> S(3).data = 5:7;
>> S(2).data
ans =
3 4 5
>> vertcat(S.data)
ans =
1 2 3
3 4 5
5 6 7
Thank you for your help. I am using the same method later on to store the corresponding binarised dataset (sizes 375x91x223) for the images as well. For example:
data2.(['val' num2str(k)]) = dataset
I am storing this in 'data2' and I want to also write a for loop to go through 'data2.val1', going to 'data2.val20' to execute a different script for the corresponding datasets i.e. for k=1:20 data.val...(value of k for iteration). I have tried doing it using this method but obtain errors 'Subscripted assignment dimension mismatch.'
for k = 1:20
S(k).val = ...
end
Thank you for your guidance! It worked with data{k}.
Stephen23
Stephen23 2016-8-4
编辑:Stephen23 2016-8-4
@ajk1: I am glad you got it working. Note that you used a cell array, whereas a non-scalar structure may have advantages for your situation.
I agree, when I use data(k) I obtain an assignment error. For data(k).val=dataset. The error I obtain is 'Structure assignment to non-structure object.' used within the loop.
You are getting this error because data already exists (probably as a cell array), and then you try to access it as it it were a structure.
Do this:
data = struct();
for k = 1:20
data(k).val = ...
end
I am trialing using this with a dataset of the same size. By running
d=zeros(375,91,223)
data = struct();
for k = 1:20
data(k).val = d
end
It doesn't store the variable after each iteration. It could be that I'm not sure how to correctly access it.
" I'm not sure how to correctly access it"
That is why I gave the link in my very first comment. It tells you how to access data in a non-scalar structure. Did you read it ?
Create a non-scalar structure:
>> S = struct();
>> for k = 1:3, S(k).val = k:2+k; end
access the data:
>> S(2).val % get the second value
ans =
2 3 4
>> vertcat(S.val) % get all values, concatenate together
ans =
1 2 3
2 3 4
3 4 5
ajk1
ajk1 2016-8-4
编辑:ajk1 2016-8-4
Perfect, can this also be applied to datasets of different dimensions (i.e. 375x91x223, 375x61x223x... ? I have also tried to apply this method to another part of my code but am having difficult accessing them with this method as the data is not stored in the corresponding iteration number. Thanks.
@ajk: a non-scalar array is like any MATLAB array: it can have whatever size you want. Just use indexing like you normally would:
S(375,91,223).val = ...
Note that if you are using such large arrays then you should preallocate them, perhaps like this:
S = struct();
S(375,61,223) = struct(); % preallocate final array size
That works perfectly. Thank you very much for your help!
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.
Okay, I have solved this. Thanks again!

请先登录,再进行评论。

 采纳的回答

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 个评论

This works for both the image and the corresponding dataset. For the datasets I changed k=1:numel(data). Thank you!

请先登录,再进行评论。

更多回答(1 个)

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 个评论

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.'
It's working now! Thanks again.

请先登录,再进行评论。

类别

帮助中心File 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