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

6 次查看(过去 30 天)
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?

采纳的回答

MathWorks Support Team
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']);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Scripts 的更多信息

产品


版本

R2007a

Community Treasure Hunt

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

Start Hunting!

Translated by