Azzera filtri
Azzera filtri

How can I redirect a script to take user input from a file instead of the keyboard?

3 visualizzazioni (ultimi 30 giorni)
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.

Risposte (4)

Walter Roberson
Walter Roberson il 5 Mag 2015
Yes, it is possible, provided that you are using input(). It works better if you have no graphics.
Simple example:
$ cat
matlab -nodesktop -nosplash -r "try; vs = input('','s'); v =sscanf(v, '%f'); testfun(v(1),v(2)); end; quit;"
$ < 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
  1 Commento
dpb il 5 Mag 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.

Accedi per commentare.

dpb il 4 Mag 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
Star Strider il 4 Mag 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
Kelly Kearney il 4 Mag 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:
% myscript.m
x = input('Enter x: ');
y = input('Enter y: ');
... x + y, or whatever ...
Change it to
function myscript(x,y)
... 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
myscript(x(ii), y(ii);


Scopri di più su Startup and Shutdown in Help Center e File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by