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.

采纳的回答

Walter Roberson
Walter Roberson 2011-6-19
Please see this FAQ
  1 个评论
Jan
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 CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by