How to change variable name in MAT file

조회 수: 25 (최근 30일)
Andrey Melnikov
Andrey Melnikov 2023년 4월 28일
댓글: Andrey Melnikov 2023년 5월 5일
Hi!
I wrote a code which make some files *.mat. All files were made with loop, therefore every file have one variable inside with same name but different contain. And when a load mat files to the next script every file change the variable contain from previous file.
Of cours I can open every file and change the variable name manualy, but how can I automatize this process?
Thanks a lot for the answer.
  댓글 수: 2
Walter Roberson
Walter Roberson 2023년 4월 28일
It is often easier to code processing of files in a loop if the variable name is the same.
Stephen23
Stephen23 2023년 4월 29일
편집: Stephen23 2023년 4월 29일
"All files were made with loop, therefore every file have one variable inside with same name but different contain."
What you describe is good data design. Much much better than having a different variable name in every MAT file.
"Of cours I can open every file and change the variable name manualy, but how can I automatize this process?"
Why all the effort just to ruin good data design and make your data harder to work with?
What would you do with lots of separate variables in the workspace? Processing them will be harder, slower, and much uglier:
Solution: keep the variables the same, just like all experiences MATLAB users would do.
Learn to LOAD into an output variable:
S = load(...);
and store the imported data in one array using indexing, just as the MATLAB documentation shows:
Then you can write simple, robust, efficient MATLAB code.
What you are proposing is the opposite. Best avoided.

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

채택된 답변

Fangjun Jiang
Fangjun Jiang 2023년 4월 28일
편집: Fangjun Jiang 2023년 4월 28일
Save the .mat file as what it is from the source. Use a=load('*.mat') to load it. "a" would be a structure. The variable is in a.VarName.
  댓글 수: 3
Fangjun Jiang
Fangjun Jiang 2023년 5월 5일
??
Andrey Melnikov
Andrey Melnikov 2023년 5월 5일
I mean that I tried, it works, thanks

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 4월 28일
You could modify the File Exchange Contribution https://www.mathworks.com/matlabcentral/fileexchange/42274-loadfixnames-loads-a-mat-file-fixing-invalid-names to patch .mat variable names.
This is not something I would recommend. For many purposes, the situation you have now with the same variable name in each file is easier to deal with.
  댓글 수: 2
Andrey Melnikov
Andrey Melnikov 2023년 5월 5일
"This is not something I would recommend. For many purposes, the situation you have now with the same variable name in each file is easier to deal with. "
I understand but the next stage I need to use content from each variable together simultaneously.
Thanks a lot
Stephen23
Stephen23 2023년 5월 5일
편집: Stephen23 2023년 5월 5일
"...the next stage I need to use content from each variable together simultaneously."
That is easier, more robust, and more efficient when the variable name/s are exactly the same in every MAT file.
Import the data into one array (which could be a container array, e.g. structure or a cell array), which will make processing your data simpler and more efficient than trying to process lots of variables. This is exactly what the MATLAB documentation shows:
Having them in one array makes using them "simultaneously" trivial.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by