Making a Loop with Strings

73 次查看(过去 30 天)
Hello all,
What is the quick way of shortening this code?
Thanks in advance for the help (I'm sure this one's not 'hard' but I don't know the 'easy' way--or anyway).
data_10_100_1=importdata('filename_2.txt');
data_10_100_2=importdata('filename_5.txt');
data_10_100_3=importdata('filename_9.txt');
data_10_100_4=importdata('filename_11.txt');
data_10_100_5=importdata('filename_14.txt');
data_10_100_6=importdata('filename_16.txt');
data_10_100_7=importdata('filename_17.txt');
data_10_100_8=importdata('filename_18.txt');
data_10_100_9=importdata('filename_20.txt');
data_10_100_10=importdata('filename_22.txt');
data_10_100_11=importdata('filename_23.txt');
data_10_100_12=importdata('filename_24.txt');
data_10_100_13=importdata('filename_28.txt');
data_10_100_14=importdata('filename_29.txt');
The answer will be something like:
x = [2 5 9 11 14 16 17 18 20 22 23 24 28 29];
for i =1:14;
data_10_100str2num(i) = importdata('filename_' num2str(x(i)) '.txt')
end; clear i
but this doesn't work... I'm not sure how to fix it. HOw do I make that LHS correct (the RHS is probably also wrong..)
Many thanks, M.A.B.

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2013-9-16
编辑:Azzi Abdelmalek 2013-9-16
Use a cell array
x = [2 5 9 11 14 16 17 18 20 22 23 24 28 29];
for i =1:14;
data_10_100{i} = importdata(sprintf('filename_%d.txt',x(i)));
end;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by