help with concatenation / eval

7 visualizaciones (últimos 30 días)
lucas
lucas el 8 de En. de 2014
Comentada: lucas el 8 de En. de 2014
Hello...
I need to load several files with specific initials and numbers (ex: luc_1.txt to luc_10.txt) to further create variables from each column of those files
Im thinking of using "eval" to create variables, but i dont know how can i get the name of those variables that i've created
im thinking about something like this:
num_part = input ('how many participants do u want to load? ');
num = input ('type the number of trials: ');
np = 1;
for part = np : num_part
np = np+1;
initials = input ('type the initials of the participant: ', 's');
weigth = input ('type the weigth of the participant: ', 's');
n = 1;
for tentativ = n : num
name = [initials,'_',num2str(n)] ;
name_file = [name,'.txt'] ;
load (name_file) ;
n = n+1;
eval([name,'fA = ' name ' (:,1:2) ;'])
eval([name,'fM = ' name ' (:,3:4) ;'])
eval([name,'fV = ' name ' (:,5:8) ;'])
eval([name,'cX = ' name ' (:,9) ;'])
eval([name,'cY = ' name ' (:,10) ;'])
eval([initials,'weigth = ' weigth ' ;'])
end
end
how do i get the name of those variables tht i' created w/ eval? any suggestions?
thx :)

Respuesta aceptada

Jos (10584)
Jos (10584) el 8 de En. de 2014
Avoid eval!
You want to use cell or structs. For example:
initials = 'AB'
weight = '171 lb'
for n = 1:3
name_file = [initials '_' num2str(n) '.txt'] ;
RAWDATA = load(name_file) ;
data.(initials).values(n).fA = RAWDATA(:,1:2) ;
data.(initials).values(n).fM = RAWDATA(:,3:4) ;
end
data.(initials).weight = weight ;
  1 comentario
lucas
lucas el 8 de En. de 2014
hey Jos! thx for the help! But i never studied those structs stuff before!
I need to create a .txt with some calculations w/ those columns of each file
can i create a single .txt for all the participants, like:
fA.txt:
luc_1.txt luc_2.txt joe_5.txt bob_3.txt
data data data data
or something like this
luc_1_fA.txt, luc_2_fA.txt, joe_5_fM.txt.....
?
thanks :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by