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

2 次查看(过去 30 天)
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!

回答(1 个)

Jan
Jan 2015-4-6
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)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by