How can I run scripts with the same name, that are contained in different folders?

7 views (last 30 days)
I have a folder structure like the following:
C:\Work\Version1\myFile.m
C:\Work\Version2\myFile.m
If my working directory is C:\Work\, how can I specify which version of myFile.m should be executed?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
This can be done using the RUN function availabe in MATLAB by specifying the folder and filename to be executed in the RUN command. for example,
run('C:\Work\Version1\Myfile'); % to execute the file in 1st folder
run('C:\Work\Version2\Myfile'); % to execute the file in 2nd folder
Another possibility is to use anonymous functions, as follows:
anon1 = @() run('C:\Work\Version1\Myfile');
anon2 = @() run('C:\Work\Version2\Myfile');
anon1() % Version 1 executed
anon2() % Version 2 executed
A third method to do the same thing is shown below:
actualPath = 'C:\Work\Version1\'
run([actualPath 'myFile']); % With or without .m extension.
actualPath = 'C:\Work\Version2\'
run([actualPath 'myFile']);

More Answers (0)

Categories

Find more on Scripts in Help Center and File Exchange

Products


Release

R2007a

Community Treasure Hunt

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

Start Hunting!