Borrar filtros
Borrar filtros

Making a Loop with Strings

80 visualizaciones (últimos 30 días)
Michael
Michael el 16 de Sept. de 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.

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 16 de Sept. de 2013
Editada: Azzi Abdelmalek el 16 de Sept. de 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 comentarios
Michael
Michael el 16 de Sept. de 2013
Thank you kindly! I'll be able to use this forever now! Your help is much appreciated.
Alisson Vinicius Brito Lopes
Alisson Vinicius Brito Lopes el 5 de En. de 2021
Thank you !

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by