MATLAB: Renaming variable loaded from a file

varibles renaming

Hello, I have 4 .mat files including tens of variables of the same name. How I can rename the variables after loading each file in a new script file. I have read similar link but didn't work well.
Thanks

Best Answer

Use either the load or matfile functions with an output argument. The structure format can keep the variable names separate.
For instance, if you have variable names ‘x’, ‘y’, and ‘z’ in each .mat file, you can load them all into your workspace without ambiguity. If your .mat files are named A.mat and B.mat, load them as:
A = load('A.mat');
B = load('B.mat');
then:
A.x % ‘x’ from A.mat
B.x % ‘x’ from B.mat
and so forth for the rest of them. You have to refer to them as such in the rest of your code, but they will be uniquely identified that way.