How can I call a function that has an input variable?
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Shaun Hamilton
am 31 Okt. 2018
Bearbeitet: madhan ravi
am 1 Nov. 2018
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 Kommentare
Akzeptierte Antwort
madhan ravi
am 31 Okt. 2018
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 Kommentare
madhan ravi
am 1 Nov. 2018
Bearbeitet: madhan ravi
am 1 Nov. 2018
Thank you Guillaume for the crystal clear explanation, I am not a good explainer :) have to work on this part.
Weitere Antworten (1)
Cam Salzberger
am 31 Okt. 2018
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 Kommentare
Siehe auch
Kategorien
Mehr zu Function Creation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!