Is it possible to write a MATLAB script that can g

2019-07-08 08:33发布

问题:

I am writing MATLAB code that will fit together with other MATLAB functions that I cannot modify. Some of these existing functions take input from the command line. Is there a way I can write a test script in MATLAB that can call these functions, and then provide the input as the user would? ie. if I have a function:

function y = f(x)
z = input('Enter number: ');
y = x + z;
end

Is there a way to have a script call f and provide z?

回答1:

If you are looking for a non elegant solution.

If you are looking for a potentially dangerous solution.

Then you might try this: write a function named "input" as follows:

function a=input(str)
% THIS IS THE DUMMY VERSION OF THE
% MATLAB BUILT-IN FUNCTION "input"
global dummy_input

disp('WARNING!!!')
disp('MATLAB "input" built-in function overridded')

disp(['Setting dummy_inpt'])
a=dummy_input;
end

Declare a global variable either in the script you use to test the function and in your "dummy" input function.

Assign the desired value to the global variable as follows:

global dummy_input

x=3;

dummy_input=123;

y=my_func(x)

dummy_input=42.13;

y=my_func(x)

If my_func is the function you post in the question, you will obtain:

WARNING!!!
MATLAB "input" built-in function overridded
Setting dummy_inpt

y =

   126

WARNING!!!
MATLAB "input" built-in function overridded
Setting dummy_inpt

y =

   45.1300

I've added the printing of the warnings in the "dummy" input function yust as a remainder ...

You do not need to modify the function you want to test, when it will call input to get a number from the user, it will call your "dummy" input.

Version 2 of the "dummy" input function

This version of the "dummy" input function allows autonatically handling multiple request of input values.

It requires the user knows in advance how many times the "original" input function is called.

No additional global counter is required.

It is sufficient the change the definition of the global parameter in the script, declaring it as an array containing the set of input the user want to assign:

global input_list
input_list=[27    30     5    31    21]

In the "dummy" input function, the first element of the array is assigned to the output variable, then the it is deleted:

a=input_list(1);
input_list(1)=[];

the code of the updated version of the function is the following:

function a=input(str)
% THIS IS THE DUMMY VERSION OF THE
% MATLAB BUILT-IN FUNCTION "input"
global input_list

disp('WARNING!!!')
disp('MATLAB "input" built-in function overridded')

disp(' ')
disp(' ')
disp(' ')
if(isempty(input_list))
   error('Error in DUMMY input: no more input data')
else
   disp(['Setting dummy_input ' num2str(input_list(1))])
   a=input_list(1);
   disp(' ')
   disp(' ')
   disp(' ')

   input_list(1)=[];
end

end

An error is generated in case the input array becomes empty (by deleting its element at each call) before the end of the script.

I've also added some calls to disp to make more "clear" the output on the Command Window.

Also the "dummy" input function print a message on the Command Window telling which input values has been assigned.

Make sure to remove your dummy "input" function at the end

Hope this helps.