Borrar filtros
Borrar filtros

How to change variable name in MAT file

15 visualizaciones (últimos 30 días)
Andrey Melnikov
Andrey Melnikov el 28 de Abr. de 2023
Comentada: Andrey Melnikov el 5 de Mayo de 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 comentarios
Walter Roberson
Walter Roberson el 28 de Abr. de 2023
It is often easier to code processing of files in a loop if the variable name is the same.
Stephen23
Stephen23 el 29 de Abr. de 2023
Editada: Stephen23 el 29 de Abr. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 28 de Abr. de 2023
Editada: Fangjun Jiang el 28 de Abr. de 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 comentarios
Fangjun Jiang
Fangjun Jiang el 5 de Mayo de 2023
??
Andrey Melnikov
Andrey Melnikov el 5 de Mayo de 2023
I mean that I tried, it works, thanks

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 28 de Abr. de 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 comentarios
Andrey Melnikov
Andrey Melnikov el 5 de Mayo de 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 el 5 de Mayo de 2023
Editada: Stephen23 el 5 de Mayo de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by