How can I call a function that has an input variable?
16 次查看(过去 30 天)
显示 更早的评论
I am wanting a script to use data from a function. However, because this function has the 'input' command in it, every time I call the function, the command window asks for the input. I am wanting the script to choose the desired input, and not the user. Is there a way to call the function, and bypass the 'input' command, by allowing the script to insert a value for the variable?
The function looks as follows:
function [output] = myfunc(variable)
% Function
clear;
clc;
% Ask user for variable.
variable = input('Input variable: ');
% Initialise FOR loop
output = 0;
% Setup IF...ELSE statement
if variable ~= round(variable)
fprintf('That is not a valid variable.')
variable = input('Input a positive integer: ')
else
for ii = 1:variable
a = 2.^(ii-1);
output = output + a;
end % End FOR loop
fprintf('%d and %d \n',variable,output)
end % End IF...ELSE
end % End FUNCTION
The script would need to call the function in some way that another input could extract data from the function and use the data in a WHILE loop.
Out = input('Input: ');
n = 1;
a = 1;
while Out > b
b = @(n) myfunc(variable)
n = n + 1
end
That is an example of what I have tried, but even with other iterations I have had, calling the function results in the need for the user to input a value for the 'variable'.
0 个评论
采纳的回答
madhan ravi
2018-10-31
variable = input('Input variable: ');
The above should be outside the function and then this can be passed as an argument to the function by calling , as simple as that.
8 个评论
madhan ravi
2018-11-1
编辑:madhan ravi
2018-11-1
Thank you Guillaume for the crystal clear explanation, I am not a good explainer :) have to work on this part.
更多回答(1 个)
Cam Salzberger
2018-10-31
Hey Shaun,
I am assuming you cannot control this function's code - you are writing a script to evaluate someone else's code or something like that?
In that case, and this is kind of a terrible thing to do, you could write your own input function that you put higher on the MATLAB search path than the built-in input. Your input function would look something like:
function x = input(varargin)
x = 1;
end
This should cause the function's input call to call this function instead. However, this would only work for always returning the same input to the function. You could make this function smarter, providing a return value based on the input to input, or use a persistent variable to track the number of times input was called or something.
I'm not sure what is going on with the script calling input as well. Is that code you control or not? If so, I'd recommend not using input, and instead making the script into a function you can pass an argument to. If not, maybe you can use the techniques mentioned above to adjust the output as needed.
Something to consider, anyway.
-Cam
2 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!