How can I change a matrix name in workspace during my script ?

4 visualizaciones (últimos 30 días)
N/A
N/A el 22 de Jul. de 2022
Comentada: Stephen23 el 22 de Jul. de 2022
Hello together,
I am new here and i'm trying to rename matrix (6x7 size) called matrixuplink and matrixdownlink in the workspace with a specific name which comes from inputs.
The only way i found to do this is to defined manually the name in my script and tells that this value is equal to my matrix. I wanted to know if is it possible to do this by giving the name from inputs ?
My matrix final name is defined with two inputs : name and chX.
Thank you in advance for yours answers.

Respuesta aceptada

Stephen23
Stephen23 el 22 de Jul. de 2022
Editada: Stephen23 el 22 de Jul. de 2022
The problem is caused by your data design, which forces meta-data into variable names. Forcing meta-data into variable names makes accessing data slow, complex, and fragile:
Your code would be simpler, more efficient, and more robust if you stored meta-data (which is data after all) in some variables rather than in the variable names. That is the recommended approach.
However, if you wish to continue with this slow, complex, fragile approach which will break when the user enters invalid characters, then you can do so by creating them as fields of a structure:
tmp = struct();
fnm = sprintf(..);
tmp.(fnm) = someArray;
and then saving that structure using the '-STRUCT' option (this stores the fields as individual arrays):
save(filename,'-struct',tmp)
But note that your code would be simpler, more efficient, and much more robust if you simply stored all of that meta-data in some variables and used exactly the same variable names in all of your MAT files:
save(filename,'someArray','chX','name')
Note that you should always LOAD into an output variable:
S = load(..)
  2 comentarios
N/A
N/A el 22 de Jul. de 2022
Hello Stephen,
Thank you for your answer, works pretty well. I will take a look to the forcing meta-data into variable names method.
Stephen23
Stephen23 el 22 de Jul. de 2022
"I will take a look to the forcing meta-data into variable names method."
Even better would be taking a look at not forcing meta-data into variable names.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by