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 :)
0 个评论
采纳的回答
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 ;
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!