Executing .m files function in R2015b is not working properly
Show older comments
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!
11 Comments
You forgot to tell us some of the most important information, whether those M-files are scripts or functions. In either case you should avoid the counter-productive eval and the slow-and-pointless copying-and-deleting of files:
- scripts -> call run.
- functions -> use str2func to generate a function handle and call that.
You should avoid cd as much as possible.
Lluis Ballber Mayans
on 13 Jun 2019
Rik
on 13 Jun 2019
Why did someone make the mistake of putting data in the filename, and why are you not allowed to move that information to a better place? Jumping through hoops has already bitten you in the butt, you should expect that to happen again.
Also, if you use run instead of eval in your code, what happens?
Guillaume
on 13 Jun 2019
"someting wrong happends"
More details needed. If you get an error message, give us the full text of the error message. Otherwise, tell us what you expected to happen and what happened instead.
Considering that names starting with numbers has never been valid script, function or variable names in matlab, why was this ever used? At the very least, going forward you could ensure that the numbers are after the text part.
As has been said, there's never any reason to cd into anything. Just use full paths. Once you've renamed your files, you also don't need eval. run will work just as well.
Lluis Ballber Mayans
on 13 Jun 2019
Rik
on 13 Jun 2019
Is there any pattern in which file is not run?
Also, you should really consider convincing the person who made this mess to make them valid script names by starting with a letter and replacing decimals with an underscore.
Joel Handy
on 13 Jun 2019
Edited: Joel Handy
on 13 Jun 2019
Sounds like maybe one of two things may be happening. One, there is an issue with one of the scripts being run. Two, you have a race condition where matlab is trying to execute commands faster than your file system can respond.
Copyfile outputs a success flag. I recomend checking this flag to verify all of your copy commands are executing successfully. You could also try putting "pause(1)" between your copyfile, run, and delete calls and see if that clears up the issue.
If it is a race condition, you might consider creating a subdirectory, and copying all the files there with a fixed name, running all of the scripts, then deleting the directory. That would alleviate the need for any arbitrary pauses.
Race conditions, system response time, pause, copyfile, eval, delete, etc. etc.
Keep in mind that all of these are just fragile-behaviors/slow-hacks due to badly named files.
In case it is not clear enough by now: using standard valid MATLAB names would mean that you would not need any hacks at all.
@Lluis,
We are trying to help. We are also trying to understand fully your situation. At the moment, we don't have enough information to solve your problem. I'm also not criticising, I'm trying to understand why you're trying to run m files that are not and have never been valid matlab files. It's also not clear why you can't just permanently rename all the files (inside or outside matlab) to make them valid matlab files. We're also interested in helping people develop good code, so expect to see suggestions to not create the mess in the first place. "I'm creating matlab files that are not compatible with matlab" is a strange thing to do.
It probably would help if you attached an example file (one that does not appear to be executed).
One thing to consider with your current code is that you rename each file to the same name copy.m. What if instead you generate a unique name for each file using e.g tempname? If that solve the problem, then what you're seeing may be due to caching (OS or matlab).
Lluis Ballber Mayans
on 14 Jun 2019
I'm not sure many here have 2015b installed anymore so it's going to be difficult to diagnose the issue. Other than the fact that the whole thing completely violates all programming best practices I can't see much wrong with it, with the exception of the regular expression in whos. Maybe the behaviour of whos changed between 2015b and 2016a.
Note that your regular expression matches any variable starting with P (or V) followed by 0 or more _. If you meant to match P (or V) followed by one _ and any character the regexp is ^P_.*. You could combine both whos into one with:
prmfound = whos('^[PV]_.*', '-regexp'); %you actually don't need the .*, so '^[PV]_' would work as well
However, if all the scripts you want to run follow the same format (variable creation followed by field assignments) I would recommend a completely different approach to importing them. I would actually parse the files which would avoid having to rename them, avoid eval, and avoid the risk of a file accidentally stomping on existing variables.
Accepted Answer
More Answers (0)
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!