How to change variable name in MAT file

19 vues (au cours des 30 derniers jours)
Andrey Melnikov
Andrey Melnikov le 28 Avr 2023
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 commentaires
Walter Roberson
Walter Roberson le 28 Avr 2023
It is often easier to code processing of files in a loop if the variable name is the same.
Stephen23
Stephen23 le 29 Avr 2023
Modifié(e) : Stephen23 le 29 Avr 2023
"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.

Connectez-vous pour commenter.

Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 28 Avr 2023
Modifié(e) : Fangjun Jiang le 28 Avr 2023
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 commentaires
Fangjun Jiang
Fangjun Jiang le 5 Mai 2023
??
Andrey Melnikov
Andrey Melnikov le 5 Mai 2023
I mean that I tried, it works, thanks

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 28 Avr 2023
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 commentaires
Andrey Melnikov
Andrey Melnikov le 5 Mai 2023
"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 le 5 Mai 2023
Modifié(e) : Stephen23 le 5 Mai 2023
"...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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by