MATLAB: How to pass a string input argument when launching MATLAB with -r option
MATLAB
I would like to know how to pass in a string value, for example, a file name, when starting MATLAB with -r option.
Best Answer
Suppose you have a MATLAB function that opens a file and reads its contents using TEXTSCAN:
function myValues = readMyFile(filename)
fid = fopen(filename, 'r');
myValues = textscan(fid, '%s');
end
Use either of the following commands to pass in your filename (assuming both the file and your MATLAB function are in current DOS working directory) when starting MATLAB:
(I'm not an expert here, just a user of the -batch option) The main advantages for me of the -batch option compared to -r for non-interactive cases are:
it more concisely sets up the -nosplash and -nodisplay options (even on Windows)
removes the startup banner
ensures the MATLAB process exits (with an exit code) even if your statement throws an error
Best Answer