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:
matlab -r readMyFile('myFile.txt')
or
matlab -r "readMyFile('myFile.txt')"