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
>> 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
ajk1
2016년 8월 4일
for k = 1:20
S(k).val = ...
end
ajk1
2016년 8월 4일
ajk1
2016년 8월 4일
Stephen23
2016년 8월 4일
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'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
Stephen23
2016년 8월 4일
@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 = ...
S = struct();
S(375,61,223) = struct(); % preallocate final array size
ajk1
2016년 8월 4일
ajk1
2016년 8월 4일
ajk1
2016년 8월 4일
채택된 답변
추가 답변 (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
카테고리
도움말 센터 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!