Search code examples
matlabdirectorycommandexecutesubroutine

Open and run a .m in matlab into an another .m file (from another directory)


I have a question about a code I am creating.

I have a code in a .m file, let's name it as "first.m".

But in the "first.m" i would like to write a command in this script in which I would like to run and execute an another .m file, let's name it "second.m" file, which is in a different directory.

I mean I would like to use a subroutine, but the main problem is that I do not have understand how can I use subroutines in MATLAB.

Could anyone help me to make it?


Solution

  • try coding using the run function. It is important that Matlabs knows where to find that function. You can use the function addpath Also, you can actually copy the full path in that command. Like this:

    run('C:\Users\user\Desktop\second.m')
    

    Make sure you write the correct path that contains second.m. If the code is in the same path as first.m or you have already added the path you can use:

    run('second.m')
    

    And if they are in a subfolder of the actual folder where first.m is, you can use the first example or:

    run('subfolder\second.m')