Running Matlab with arguments

I have a matlab file that accepts a file. I would like to run this program in a matlab shell, such as prog. I need to implement it so that it takes many arguments, such as "prog filename.txt 1 2", which would mean that I can use filename.txt and 1 2 as variables in my program.

Thanks!

+6
source share
1 answer

To make script accept arguments from the command line, you must first turn it into a function that will get the arguments you need, i.e. if your script has the name prog.m , put as the first line

 function []=prog(arg1, arg2) 

and add end to the end (assuming the file has only one function). It is very important that you call the function with the same name as the file.

The next thing you need to make sure the script file is in the same place where you are calling the script, or located on the Matlab working path, otherwise it will not be able to recognize your script.

Finally, to execute the script, you use

 matlab -r "prog arg1 arg2" 

which is equivalent to calling

 prog(arg1,arg2) 

from inside Matlab.

* - tested in Windows and Linux environments

+13
source

Source: https://habr.com/ru/post/906720/


All Articles