Azzera filtri
Azzera filtri

Making a Loop with Strings

80 visualizzazioni (ultimi 30 giorni)
Michael
Michael il 16 Set 2013
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.

Risposta accettata

Azzi Abdelmalek
Azzi Abdelmalek il 16 Set 2013
Modificato: Azzi Abdelmalek il 16 Set 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 Commenti
Michael
Michael il 16 Set 2013
Thank you kindly! I'll be able to use this forever now! Your help is much appreciated.
Alisson Vinicius Brito Lopes
Thank you !

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by