MATLAB: Executing .m files function in R2015b is not working properly

.mcdcopyfiledeletedirevalfullfileMATLAB

Hi everyone!
I have a main funtion which calls ExecuteMFiles() at certain point. I also have an inputdocPath to a folder with many different files ( .dat, .m, etc.), these files have an important information in their name so it can't be changed but unfortunatelly they are written starting with numers and dots (eg "32.521_parameter.m") which I know it's one of the very first rules to not follow so what I've done is make a copy, execute it and delete it.
That's done for each .m file so I can run it without an error in MATLAB R2016b but when I use R2015b version, someting wrong happends. If I run my main code in R2015b I get an error in relation with non-seen parameters which should been defined while running ExecuteMFiles() but if I use debugging from copyfile() line and then I go step by step, all the .m file run well so I'm confused!
Could anyone help me with this? I let you here the ExecuteMFiles()'s first part (the second part is just the assigment of names for each parameter in the workspace) which I guess it's failing for something.
function ctl = ExecuteMFiles( inputdocPath )
cd (inputdocPath);
found = dir(fullfile(inputdocPath, '*.m') );
for iF = 1:length(found) %run all *.m file with parameters
copyfile(found(iF).name,'copy.m');
eval('copy');
delete('copy.m');
end
Thank you very much!

Best Answer

Ugh, invalid filenames and anti-pattern dynamic variable names... it seems that your task was explicitly designed to torture interns with! It is a lesson in how NOT to design code and data. Most likely you should actually parse the files to get the data, rather than try to run them, and so avoid this entire ugly, fragile mess.
As for your original question, try adding
clear('copy')
before run (experiment to find the best location), to clear the script from memory:
I also recommend using a name that is less likely to be used elsewhere, e.g. mycopy.
Related Question