Borrar filtros
Borrar filtros

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

1 visualización (últimos 30 días)
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.

Respuesta aceptada

Walter Roberson
Walter Roberson el 19 de Jun. de 2011
Please see this FAQ
  1 comentario
Jan
Jan el 19 de Jun. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by