MATLAB: How to load in .m files in a sequence to Matlab

.m filessequence load

I am trying to solve a problem where I have to use multiple .m files to extract information from them and modify it slightly. The problem is that I cannot load in all .m files at once as the variables would overwrite each other (all .m files conatin the same variables but at different time steps). Can anybody help me how to create a sequence specifically for .m files or which command could be used? (I tried load and importdata commands, did not seem to work)

Best Answer

"The problem is that I cannot load in all .m files at once..."
Your question is confusing: .m files contain code (and in general they are not loaded as data), whereas .mat files contain data variables (which can be loaded into MATLAB memory). M-files can be scripts or functons or classes, which can be run/called as appropriate. But usually it does not make much sense to load them.
I will assume that you actually have multiple .mat files, in which case you can easily prevent variables being overwritten in a loop by loading the .mat files into an output variable (which is a scalar structure)
S = load(...);
If all of the .mat files contain exactly the same variables, then that scalar structure can easily be joined into one non-scalar structure, which makes access all of the data easy:
N = number of files
A = cell(1,N);
for k = 1:N
A{k} = load(...);
end
A = [A{:}];