MATLAB: Load variables with same name with uigetfile and give them a new name automatically

changeloadnameuigetfilevariableworkspace

I have different variables (cells) in my folder, that have the same name when they are loaded into the workspace with "uigetfile(…)". I would like to give each variable automatically a different name (such as var1, var2, var3…for the consecutive variables), when I load them into the workspace. I started like this but I dont get any further. Can someone help me? Thank you 🙂
[FileName,PathName] = uigetfile('*.mat','MultiSelect','on');
FileName=FileName';
for i=1:size(FileName,1)
var = load([PathName cell2mat(FileName(i))])
end
somehow I want to give to "var" a different name in every iteration (such as var1 in the first iteraton, var2 in the second and so on…)

Best Answer

Don't do that!
The better approach to this problem is to use cell array or array of structs. I'd suggest array of structs for this case.
[FileName,PathName] = uigetfile('*.mat','MultiSelect','on');
for k=1:numel(FileName)
var(k).data = load(fullfile(PathName,FileName{k}));
end
Related Question