How to change variable name in MAT file
21 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Andrey Melnikov
il 28 Apr 2023
Commentato: Andrey Melnikov
il 5 Mag 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 Commenti
Walter Roberson
il 28 Apr 2023
It is often easier to code processing of files in a loop if the variable name is the same.
Stephen23
il 29 Apr 2023
Modificato: Stephen23
il 29 Apr 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.
Risposta accettata
Fangjun Jiang
il 28 Apr 2023
Modificato: Fangjun Jiang
il 28 Apr 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 Commenti
Più risposte (1)
Walter Roberson
il 28 Apr 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 Commenti
Stephen23
il 5 Mag 2023
Modificato: Stephen23
il 5 Mag 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.
Vedere anche
Categorie
Scopri di più su Workspace Variables and MAT-Files in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!