How can I redirect a script to take user input from a file instead of the keyboard?
3 views (last 30 days)
Show older comments
Ranjan Sonalkar on 4 May 2015
Commented: dpb on 5 May 2015
I have a MATLAB script that asks for multiple user inputs in a sequence and then uses them as variables within the script. The script takes about an hour or more to finish. I would like to run the script multiple times, each time with a different set of inputs that I would like to pre-populate in a text file. This way I can start off the string of multiple runs and go do something else while it is running.
I have not been able to find a way to do this, which I am sure must exist. I just have not searched in the right places.
Walter Roberson on 5 May 2015
Yes, it is possible, provided that you are using input(). It works better if you have no graphics.
$ cat run_test.sh
matlab -nodesktop -nosplash -r "try; vs = input('','s'); v =sscanf(v, '%f'); testfun(v(1),v(2)); end; quit;"
$ run_test.sh < inputfile1.txt
If you are using MS Windows you would do the equivalent using a .bat file, which would look very similar except without the #!/bin/sh line
dpb on 5 May 2015
Well, good to know (and I was too lazy to try), Walter. Thanks...I really hadn't thought about using the batch mode w/ -nodesktop option.
dpb on 4 May 2015
Modify the script to read a file instead; I don't think you can use command line redirection w/ Matlab (outside a compiled .exe, maybe).
You can use a double set of files; the first one with the script to start it with a list of files for cases each containing the needed data for the case.
Star Strider on 4 May 2015
I would create a function from the code in the script and save it as a separate (probably similarly-named) function file, with the input arguments being the normal user inputs. Then create a script file that reads the text file and uses a for loop to call the function file with a different set of arguments (read from the text file) in each iteration. Have the function save its outputs as a .mat or .txt file to read later (either different file names or appended to the original file), with the input values included (so you know what inputs produced the outputs). For separate files in a similar situation, I used date strings to differentiate them.
Kelly Kearney on 4 May 2015
Unless your input data is already in a separate file, it would probably be easier to just add that to your calling script, after changing your original script to a function as others suggested.
For example, if your script looks like this:
x = input('Enter x: ');
y = input('Enter y: ');
... x + y, or whatever ...
Change it to
... x + y, or whatever ...
Then write a separate script that calls it with the proper data
x = [1 2];
y = [3 4];
for ii = 1:length(x)
fprintf('Running iteration %d\n', ii); % Handy to keep track of run time
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!