how to create a matrix named after a string and be able to use it afterwards
1 次查看(过去 30 天)
显示 更早的评论
Hello, first thanks to any help you can give me.
I would like to build a m*n matrix of zeros with its var named by a string and be able to call it after to populate the zeros with numbers.
I have managed to do it using eval, like in this example:
>> eval(['my_var_name' '=zeros(4,4)'])
my_var_name =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
>> eval(['my_var_name(2,:)' '=[1,2,3,4]'])
my_var_name =
0 0 0 0
1 2 3 4
0 0 0 0
0 0 0 0
but is there a better or simpler way to do it? I will have to do this several times in a loop to a certain number of matrices that i will name by strings, the calculations i have to do in order to populated these matrices are computational expensive will eval function make any significant overburden?
thanks for any suggestions.
0 个评论
采纳的回答
Walter Roberson
2011-6-19
1 个评论
Jan
2011-6-19
+1. And to repeat it here directly: It is recommended to avoid this technique. Use dynamic fieldnames instead:
data.('my_var_name') = zeros(4); data.('my_var_name')(2, :) = 1:4;
This will reduce the time needed for programming, debugging and running your program substantially.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!