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

조회 수: 7(최근 30일)
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.

채택된 답변

Stephen23
Stephen23 2022년 7월 22일
편집: Stephen23 2022년 7월 22일
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
Stephen23
Stephen23 2022년 7월 22일
"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.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by