Naming Vectors with Nested For Loop Variables

5 views (last 30 days)
Lindsey
Lindsey on 6 Aug 2014
Answered: Iain on 6 Aug 2014
I am currently working on a project where I have multiple data sets. I have one excel file that I have loaded into Matlab that is broken down into several components. I have 10 Patients and each patient has 4 Different Conditions. Within Each condition there are 8 1x3 vectors, signifying three coordinates (x,y,and z) My question is...Is there a way to name each vector based on both patient # and condition #?
For example, if I would like to do computations on Left_Ankle_Coordinates for Patient 2 with condition 2, is there a way to name it in a way where I know both the patient # and condition #, like Left_Ankle_Coordinates_2_2? I would like to put all of this in a for loop and then write to excel.
Any help would be greatly appreciated!

Answers (1)

Iain
Iain on 6 Aug 2014
You're asking for something like this, modified for exactly what you want (i.e., the real data, rather than ones).
list1 = {'left','right','top','bum'};
list2 = {'A','B','C','D','E','F'};
list3 = {'1','12','34',73'};
for i = 1:5
for j = 1:6
for k = 1:4
name = [list1{i} '_' list2{j} '_' list{3}];
eval([name ' = ones(8,1);' ])
end
end
end
Its really nasty from a programming point of view. You would be much better simply indexing a large array, or cell array:
for patient = 1:10
for condition = 1:10
Left_ankle{patient}{condition} = ones(8,1);
end
end

Categories

Find more on Loops and Conditional Statements 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!