Making a Loop with Strings
78 views (last 30 days)
Show older comments
Michael
on 16 Sep 2013
Commented: Alisson Vinicius Brito Lopes
on 5 Jan 2021
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.
0 Comments
Accepted Answer
Azzi Abdelmalek
on 16 Sep 2013
Edited: Azzi Abdelmalek
on 16 Sep 2013
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;
3 Comments
More Answers (0)
See Also
Categories
Find more on Data Type Conversion 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!