Executing .m files function in R2015b is not working properly

2 views (last 30 days)
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
Lluis Ballber Mayans
Lluis Ballber Mayans on 14 Jun 2019
@ Guillaume
First of all, sorry for what I said to you before, I felt attacked and I was triggered with this, I have been fighting it for 2 long days and I continue stacked, I hope you understand it.
@Dear all
As an intern I could not change anything but for sure that I told them that the use of numers at the beggining and dots in a name's file was a big big mistake, but their old program export them like that so I have to deal with it...
One of my first trials was using pause because I guessed the same but I tried with 4 seconds (which is crazy) and still didnt worked as it should. I have designed a silly example which I hope it clarifies what's my problem (I should do it before, I'm sorry...).
Main
% Main code
indocPath='...\inputDoc';
y=ExecuteMfiles(indocPath);
length(struct2cell(y))==4
Note: Don't forget to put your own path!
ExecuteMFiles
function y = ExecuteMfiles( inputdocPath )
found = dir(fullfile(inputdocPath, '*.m') );
for iF = 1:length(found) %run all *.m file with parameters
copyfile(fullfile(inputdocPath,found(iF).name),'copy.m');
run('copy');
delete('copy.m');
end
% collect variables starting with P_ and V_
prmfound=[whos('^P_*','-regexp'); whos('^V_*','-regexp')];
for ip = 1:length(prmfound)
y.(prmfound(ip).name) = eval([prmfound(ip).name '.Value']);
end
23.156_fparameter
P_1 = Simulink.Parameter;
P_1.Description = ' Parameter 1';
P_1.Value = 0.01;
P_1.DocUnits = 's';
P_2 = Simulink.Parameter;
P_2.Description = 'Parameter 2 ';
P_2.Value = 4;
P_2.DocUnits = 's';
23.156_sparameter
P_3 = Simulink.Parameter;
P_3.Description = ' Parameter 3';
P_3.Value = 80;
P_3.DocUnits = 'rpm';
P_4 = Simulink.Parameter;
P_4.Description = ' Parameter 4';
P_4.Value = 955;
P_4.DocUnits = 'rpm';
I also forward you the folder with the structure I have to follow so you just download it and run it.
Capture.PNG
As you can see, we have 4 params but if you run the main code just get 2 (the first ones) but now put your debugging mark on the line which says " copyfile(fullfile(inputdocPath,found(iF).name),'copy.m') " then run it, go step by step until we go out of the first loop and then click continue. We have 4 params now, the code is ok! :O
Thanks again for your time.
Guillaume
Guillaume on 14 Jun 2019
Edited: Guillaume 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.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 14 Jun 2019
Edited: Stephen23 on 14 Jun 2019
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.
  4 Comments
Stephen23
Stephen23 on 14 Jun 2019
Edited: Stephen23 on 14 Jun 2019
"should I modify something about my post?"
No, you can leave the question just as it is. It is standard practice on this forum to use comments to add information and clarifications, which you have already done as part of our discussions about your topic.
Go and enjoy your intern-torture!
Guillaume
Guillaume on 14 Jun 2019
I will try to use all the tricks you all said to me
I would strongly recommend writing your own parser for the files instead of executing them. If the files are as simple as the one you've shown it could be a very simple parser. As I said, that would completely remove the need for renaming the files and would also remove the risk of stomping over existing variables. As it is any file that has any of:
y = ...
ip = ... %an ip address for example!
prmfound = ...
will break your code when it's eval'ed

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2015b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!