MATLAB: Rename workspace variables

MATLABvariables change nameworkspace

I have two .mat's that each have a list of vectors with the same name. I want to rename the variables of the first .mat to originalname_Ref, and then save all vectors in one .mat. But how do I change the names of the workspace vectors?

Best Answer

When you load the .mat file, load it to a variable:
x = load('mymat.mat');
names = fieldnames(x)
for iname = 1:length(names)
x.(['new_', names{iname}]) = x.(names{iname});
x = rmfield(x, names{iname});
end
save('newmymat.mat', '-struct', 's');