call a matlab script in a script

本小妞迷上赌 提交于 2021-02-18 04:53:42

问题


I have two matlab script files .m (not function files) and if I want to call another script in my current script, which command should I use? Thank you.


回答1:


I found the answer.

Just name the script in the other script:

myOtherScript

You can use run('myOtherScript') if you prefer, but it will end up internally doing the same thing as naming it directly; you might, though, like the emphasize that it gives that it is a script being mentioned.




回答2:


If you want to pass parameters to it, enclose them in parentheses.

angle=.78; bias=.001; 
myOtherScript(angle, bias)

If you want to return parameters from it, do it like this:

adjustedAngle = myOtherScript(angle, bias);

Or multiple return values:

[status adjustedAngle] = myOtherScript(angle, bias);

If you don't want the return values immediately reflected to the command window (maybe this call is in a big loop and you're going to plot all the values later), be sure to put a semicolon after the call statement.




回答3:


As you said, if your script2 is in the same folder as your script1, you can call it with its name. script2

If it is in another folder, you can use 'run'. run("../path/to/your/script/script2")




回答4:


In script test1.m put this: function test1 disp('test 1')

aaa=111;

test2( aaa );

end

In test2.m put this, then run test1.m: function test2(aaa) fprintf('test 2 aaa=%d !!!\n', aaa ) end



来源:https://stackoverflow.com/questions/5226840/call-a-matlab-script-in-a-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!