call a matlab script in a script

后端 未结 4 1965
没有蜡笔的小新
没有蜡笔的小新 2021-02-13 03:20

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.

相关标签:
4条回答
  • 2021-02-13 03:23

    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

    0 讨论(0)
  • 2021-02-13 03:33

    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")

    0 讨论(0)
  • 2021-02-13 03:46

    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.

    0 讨论(0)
  • 2021-02-13 03:47

    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.

    0 讨论(0)
提交回复
热议问题