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

6 ビュー (過去 30 日間)
Ranjan Sonalkar
Ranjan Sonalkar 2015 年 5 月 4 日
コメント済み: dpb 2015 年 5 月 5 日
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 Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by