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

5 views (last 30 days)
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.

Accepted Answer

Stephen23
Stephen23 on 22 Jul 2022
Edited: Stephen23 on 22 Jul 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 Comments
N/A
N/A on 22 Jul 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 on 22 Jul 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!