MATLAB: Load file with changeable variable

variable

Dear all,
I would like to load some .mat file in this way:
File1 = 'D:\prj\MyComp\name.mat';
File2 = 'D:\prj\MyComp\anothername.mat';
File3 = 'D:\prj\MyComp\anotherdifferentname.mat';
namesWork = who;
outStr = regexpi(namesWork,'File');
ind = ~cellfun('isempty',outStr);
ind = ind(ind==1);
for h = 1:length(ind)
load(['File' num2str(h)])
...
end
But it returns this error message:
Error using load
Unable to read file 'File1'. No such file or directory.
Thanks in advance, JOE

Best Answer

Don't waste your life writing buggy code. Much simpler and much more reliable would be to use a cell array:
C = {...
'D:\prj\MyComp\name.mat',...
'D:\prj\MyComp\anothername.mat',...
'D:\prj\MyComp\anotherdifferentname.mat'};
for k = 1:numel(C)
S = load(C{K});
...
end
By choosing to use a simple, easy-to-understand way of writing code (i.e. a cell array) I solved your task in just a few efficient lines of code. This is explained very well in the documentation and on this forum: