Using for loop to output modified tables - how can I loop through names of tables I'm calling to be modified?

1 Ansicht (letzte 30 Tage)
Hi,
I'm using a for loop to take a list of tables (x001, x002, x003, etc.), and then modify each one to include only certain columns. For example:
a = x001([1 5 10]);
How can I get a for loop to run through the subject numbers, so that the tables are all changed in this way (need same columns from every table, so just need to switch that x001 to x002, then x003, etc.).
I'm aware of how to call different file names like the code below.
for x=100
tempData=importdata([output_num2str(x) .csv]);
end
But, I am not sure how to do this with variables where my first reference to it is calling a part of it. Hope that makes sense. Could I do something like
subject_list = {'008', '009', '010', '011', '012', '013','014','015','016','017','018',...
'019','020','021','022','023','024','025','026','027'};
numsubjects = length(subject_list); % number of subjects
for s=1:numsubjects
tableName = ([x num2str(subject_list)])
a = tableName([1 5 10]);
end
Thank you!

Antworten (1)

Jan
Jan am 6 Apr. 2015
Do not do this.
It is a massive drawback to hide an index in the name of a variable. See http://www.mathworks.com/matlabcentral/answers/57445-faq-how-can-i-create-variables-a1-a2-a10-in-a-loop.
Instead of calling the variable "x008", create a struct array:
x(1).Key = '008';
x(1).Value = 'The value of your variable';
x(2).Key = '010';
x(2).Value = 'Another value';
Then strcmp helps to find the index corresponding to a certain key:
index = find(strcmp({x.Key}, '008'})
x(index)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by