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

조회 수: 6 (최근 30일)
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.

답변 (4개)

Walter Roberson
Walter Roberson 2015년 5월 5일
Yes, it is possible, provided that you are using input(). It works better if you have no graphics.
Simple example:
$ cat run_test.sh
#!/bin/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
  댓글 수: 1
dpb
dpb 2015년 5월 5일
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
dpb 2015년 5월 4일
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 2015년 5월 4일
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 2015년 5월 4일
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);
end

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by