Running pre-loaded answers for input queries on the command line in Matlab

8 views (last 30 days)
I have a lot of .m files which have multiple input commands within them. I would like to create a script which would run these .m files without changing them but having set answers ready for the input commands which would come through. I can create script to run all the .m files I need but I would like to know if there is a way to pre-load answers to all the input requests I am going to get.
Any help would be appreciated, thanks in advance
  2 Comments
per isakson
per isakson on 5 Apr 2018
Edited: per isakson on 5 Apr 2018
AFAIK: There is no straightforward documented way to do it.
How do you want to assure that the correct response is provided to each input command?
How much code and testing is it worth?
Aaron Raighne
Aaron Raighne on 5 Apr 2018
Hi, thanks for the quick response. The correct input would have to be dependent on the string that is given with the input command which means I would need some way to read that also. I would be willing to spend a few hours creating the code to do this as it would be more fun than correct the .m files but perhaps it is too difficult/time consuming

Sign in to comment.

Accepted Answer

per isakson
per isakson on 5 Apr 2018
Edited: per isakson on 6 Apr 2018
Background: It's possible to overload the Matlab function, input, with your own function, input, which you put in a folder in the top of the search path.
>> which input -all
h:\m\cssm\input.m
built-in (C:\Program Files\MATLAB\R2016a\toolbox\matlab\lang\input) % Shadowed
This is typically not a good idea, but I cannot think of a better way to solve your problem. Make sure to remove your function, input, before you close Matlab.
Answer: Here is a small example, which illustrates my approach. It shall get you started. Create the three m-files: WrapperScript, your_script_1 and input. Run WrapperScript
>> WrapperScript
answer1
17
where
%%WrapperScript
answers = { 'prompt_1', 'answer1'
'prompt_2', 17 } ;
input( '_set_', answers );
your_script_1
and
%%your_script_1
a01 = input( 'prompt_1', 's' ) ;
disp( a01 )
a02 = input( 'prompt_2' ) ;
disp( a02 )
and
function varargout = input( varargin )
%
narginchk( 1, 2 )
%
persistent pre_loaded_answers
%
if strcmp( varargin{1}, '_set_' )
pre_loaded_answers = varargin{2};
varargout = cell(0);
else
isp = strcmp( varargin{1}, pre_loaded_answers(:,1) );
varargout = pre_loaded_answers(isp,2);
end
end
in three separate m-files

More Answers (2)

Walter Roberson
Walter Roberson on 6 Apr 2018
Other than per's suggestion of overloading input(), the general mechanism that is available is I/O redirection using the shell < operator when you run matlab. For example,
/usr/local/bin/matlab -nodesktop -r "try; myscript(5); catch; end; quit" < responses5.txt
This should even work in MS Windows.
To alter the responses depending on the prompts, you will need to use some kind of inter-process communication. On Mac and Linux this can be done using some lesser-known ksh-family operations, or it can be done by tools such as "expect". Unix as a well developed mechanism for this kind of interaction, using "pseudo-terminals" (pty)

Aaron Raighne
Aaron Raighne on 13 Apr 2018
thanks everyone

Categories

Find more on Data Type Identification in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!