help with concatenation / eval

1 次查看(过去 30 天)
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 :)

采纳的回答

Jos (10584)
Jos (10584) 2014-1-8
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 个评论
lucas
lucas 2014-1-8
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 :)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by