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!