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

5 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 27 de Jun. de 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']);

Más respuestas (0)

Categorías

Más información sobre Files and Folders en Help Center y File Exchange.

Productos


Versión

R2007a

Community Treasure Hunt

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

Start Hunting!

Translated by