Alternatives for 'eval' command?
11 次查看(过去 30 天)
显示 更早的评论
Hello. I've discovered eval command to solve some of my problems. But also I've just read that's inefficient and threaten (!del *.m") too. Any suggestion to improve my code? It's like this example, where I want to apply some algebra/functions to different fields (names_2) of main variables (names_1), which are structures. Thanks in advance.
Var_1.cast1=rand(5,10);Var_1.cast2=rand(7,20);Var_1.cast3=rand(4,8); Var_2.cast1=rand(3,10);Var_2.cast2=rand(9,20);Var_2.cast3=rand(6,8);
for k=1:2
names_1{k}=strcat('Var_',num2str(k));
end
for k=1:3
names_2{k}=strcat('cast',num2str(k));
end
for k=1:length(names_1)
v=genvarname(names_1{k});
for i=1:length(names_2)
aux_var=eval([v '.(names_2{i});']);
aux_var_2.(names_2{i})=aux_var(:,2:end)';
end
eval([v '=aux_var_2;'])
clear v aux_var aux_var_2
end
0 个评论
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!