是否可以编写可以向函数提供命令行输入的MATLAB脚本? [英] Is it possible to write a MATLAB script that can give command line input to a function?
问题描述
我正在编写与我无法修改的其他MATLAB函数配合使用的MATLAB代码.其中一些现有功能从命令行获取输入.有没有一种方法可以在MATLAB中编写可以调用这些函数的测试脚本,然后按用户的意愿提供输入? IE.如果我有一个功能:
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
有没有办法让脚本调用f并提供z?
Is there a way to have a script call f and provide z?
推荐答案
如果您正在寻找一种不太雅致的解决方案.
如果您正在寻找具有潜在危险的解决方案.
然后您可以尝试执行以下操作:编写一个名为"input"的函数,如下所示:
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
在用于测试该函数的脚本和您的虚拟" input
函数中声明一个global
变量.
Declare a global
variable either in the script you use to test the function and in your "dummy" input
function.
如下所示将所需的值分配给global variable
:
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)
如果my_func
是您在问题中发布的函数,则将获得:
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 ...
您不需要修改要测试的功能,当它调用input
从用户那里获取一个电话号码时,它将调用您的虚拟"输入.
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.
虚拟"输入功能的版本2
此版本的虚拟"输入功能允许自动处理输入值的多个请求.
This version of the "dummy" input function allows autonatically handling multiple request of input values.
它要求用户预先知道原始" input
函数被调用了多少次.
It requires the user knows in advance how many times the "original" input
function is called.
不需要其他global counter
.
在脚本中更改global parameter
的定义就足够了,将其声明为包含用户要分配的一组输入的数组:
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]
在虚拟" input
函数中,将数组的第一个元素分配给输出变量,然后将其删除:
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
如果输入数组在脚本结束之前变为empty
(通过在每次调用时删除其元素),则会生成error
.
An error
is generated in case the input array becomes empty
(by deleting its element at each call) before the end of the script.
我还添加了对disp
的一些调用,以使命令窗口"中的输出更加清晰".
I've also added some calls to disp
to make more "clear" the output on the Command Window.
虚拟" input
函数还在命令窗口上显示一条消息,告诉您已分配了哪些输入值.
Also the "dummy" input
function print a message on the Command Window telling which input values has been assigned.
请确保最后删除虚拟的输入"功能
希望这会有所帮助.
这篇关于是否可以编写可以向函数提供命令行输入的MATLAB脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!