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

7 次查看(过去 30 天)
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 个评论
per isakson
per isakson 2018-4-5
编辑:per isakson 2018-4-5
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 2018-4-5
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

请先登录,再进行评论。

采纳的回答

per isakson
per isakson 2018-4-5
编辑:per isakson 2018-4-6
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

更多回答(2 个)

Walter Roberson
Walter Roberson 2018-4-6
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 2018-4-13
thanks everyone

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by