MATLAB Answers

Is it possible to pass a MATLAB file that requires an input argument to MATLAB in batch mode under UNIX?

19 views (last 30 days)
I would like to pass a MATLAB file that requires an input argument to MATLAB in batch mode under UNIX.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jan 2010
You cannot pass a MATLAB file that requires an input argument to MATLAB in batch mode. This is not possible because when you use redirection in UNIX to pass a MATLAB file to be run in batch mode, MATLAB executes the file by reading each line as if it were typed at the MATLAB prompt. MATLAB files that require input arguments must be declared as functions, and you cannot declare a function at the MATLAB prompt.
To work around this you can create an environment variable that will contain the input argument and use the MATLAB GETENV function. For example, create the environment variable with the input argument
setenv VAR 10
and retrieve the input argument using GETENV
inputArg = getenv('VAR');
inputArg = str2num(inputArg);
myFunction(inputArg)
This will allow you to pass input arguments when using batch mode. Additionally, this can be incorporated into a shell script.
Alternatively, you can write a shell script that will take your input data file and write the necessary MATLAB file you want based on the input data file. Then, when you use redirection to run the file in batch mode, specify the name of the MATLAB file you created with your shell script.

  0 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!