Clear Filters
Clear Filters

how to create a matrix named after a string and be able to use it afterwards

1 view (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Jun 2011
Please see this FAQ
  1 Comment
Jan
Jan on 19 Jun 2011
+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.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!