Azzera filtri
Azzera filtri

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

1 visualizzazione (ultimi 30 giorni)
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.

Risposta accettata

Walter Roberson
Walter Roberson il 19 Giu 2011
Please see this FAQ
  1 Commento
Jan
Jan il 19 Giu 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by