How can I call a function that has an input variable?

133 views (last 30 days)
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'.

Accepted Answer

madhan ravi
madhan ravi on 31 Oct 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 Comments
Shaun Hamilton
Shaun Hamilton on 1 Nov 2018
Thank you. I will keep that in mind, and change what I can.
madhan ravi
madhan ravi on 1 Nov 2018
Edited: madhan ravi on 1 Nov 2018
Thank you Guillaume for the crystal clear explanation, I am not a good explainer :) have to work on this part.

Sign in to comment.

More Answers (1)

Cam Salzberger
Cam Salzberger on 31 Oct 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 Comments
Shaun Hamilton
Shaun Hamilton on 1 Nov 2018
Thank you, for the response, Cam.
I am able to control this function. However, I need to function to be able to work as it currently does, if I do change it, because it will not only be used in the script I am writing.
I control the script, as well. However, someone else should be able to input their own variable, if they ran the script.
Basically, I can change the function code, as long as it will operate almost identically to the way it currently does. I need the script to call the function, whilst, simultaneously, setting a value for the 'input' command in the function. This value will depend on what the user has put in the script.
Any other ideas?

Sign in to comment.

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!