Make Field of certain size in structure using a loop
Show older comments
Dear everyone,
I am stuck with a possibly trivial problem.
I want to create a structure that has N number of fields named X1,X2,...XN. Each of those X1,X2,...XN are of some size S, such that X1 is a field of dimensions 1xS.
This is a minimal (not) working example:
batch_list = {'batch1','batch2'};
data = [];
S = 400
for i = 1:length(batch_list)
for j = 1:S
images.(batch_list{j,i}) = {X(j)};
end
end
For i = j = 1 I get a structure called images, with a 1x1 field called batch1. So far so good.
For i = 1 and j = 2 I get a error message "Index exceeds matrix dimensions" because the field is 1x1, and therefore cannot accept j = 2. I've tried redefining the size of the field in the structure, but it gets overwritten by my code.
I am lost as how to correct this and would appreciate any input.
Thank you.
1 Comment
Stephen23
on 13 Jul 2016
>> A(1).batch = 1:3;
>> A(2).batch = 4:6;
>> A(3).batch = 7:9;
and there are lots of easy ways to access the data:
>> A(2).batch
ans =
4 5 6
>> vertcat(A.batch)
ans =
1 2 3
4 5 6
7 8 9
Accepted Answer
More Answers (0)
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!