I have a script which runs various other .m files, using...
run('filename.m');
When I run the script, the .m files are not run. I can manually run them by opening them in the workspace and clicking 'run'. I can also run the .m files by entering in the command window....
filename % (i.e. without anything beforehand, and without the .m)
Can anyone help me understand why the run command doesn't work? As best as I can tell, the File directory and working folder are setup correctly
The script works for my colleagues - The only difference I'm aware of is that I have to run Matlab as an administrator, whereas my colleagues do not

댓글 수: 7

What does this show?
which -all run
Denis Gorman
Denis Gorman 2023년 8월 22일
Thanks for your response. Command window command/response pasted below:
which -all run
C:\Program Files\MATLAB\R2022b\toolbox\matlab\lang\run.m
C:\Program Files\MATLAB\R2022b\toolbox\simulink\simulink\modeladvisor\@ModelAdvisor\run.p % ModelAdvisor method
Stephen23
Stephen23 2023년 8월 22일
편집: Stephen23 2023년 8월 22일
"When I run the script, the .m files are not run."
Please explain exactly what occurs: do you get an error message (if so, what is the complete text of the message), or any other message displayed (if so, exact text please) or some dialog box (if so, what content) or does MATLAB crash and your computer catch fire? If you don't tell us, we don't know what "are not run" means.
Denis Gorman
Denis Gorman 2023년 8월 22일
No error message is displayed, nor a dialogue. As best as I can tell, nothing happens at all.
As a test, I reduced the targeted .m file to a single line which creates a new variable in my workspace. My indicator for whether the .m file 'runs or not' (poor wording, apologies) is whether the new variable gets created in my workspace when I run the .m file
Walter Roberson
Walter Roberson 2023년 8월 22일
If you which the name (without the .m) what shows up?
If you have a fixed filename that you are expecting to be on the path (including the current directory) then most of the time you should skip using run and should just use the name .
run() is useful if there is reason to give a path as part of the name (but for whatever reason you do not want the directory on the path permanently); run() is also useful if the file name is being constructed from data (computed through an expression, pulled from a drop-down list, that sort of thing.)
Stephen23
Stephen23 2023년 8월 22일
"My indicator for whether the .m file 'runs or not' (poor wording, apologies) is whether the new variable gets created in my workspace when I run the .m file"
If the RUN command is called within a function or class method then it uses the workspace of that function or method, i.e. the variable is created inside the function/method workspace and will not be visible in the base workspace:
That could explain the behavior you are seeing:
You should also check the MATLAB evaluation status in the bottom left corner of the IDE:
If MATLAB is busy it may not update the workspace.
Denis Gorman
Denis Gorman 2023년 8월 22일
Huge thanks for the responses. Replacing 'run' with a simple call out for the filename appears to be effective. Comments noted on the appropriate/inappropriate scenarios for using run. Thanks!

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

제품

릴리스

R2022b

태그

질문:

2023년 8월 21일

댓글:

2023년 8월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by