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

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

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

Either I am not understanding something, or it does not work; I could manipulate the function in many ways to allow the script to call and input in the function. However, I need the function to work on its own, regardless of this specific script I am writing.
If I put:
variable = input(Input variable: ');
Outside the function, then I get an error saying, "Function with duplicate name 'myfunc' cannot be defined."
How would I have the input in the same script file as the function, but not within the function?
With this line outside the function ,save it as .m file with function Definition at the end if the script file
Remark: the script name shouldn’t be the same as the function name and delete unique function file which has myfunc definition.
As explained by madhan, the line
variable = input('Input variable: ');
should not be part of the function. For that matter, the lines
clear;
clc;
should also not be part of the function. clear in particular has no place in the function.
This code has clearly been written by somebody that do not understand the difference between functions and scripts and do not understand how functions work.
The function takes an input, called variable (poor name) and then promptly throws it away (with clear) and ask the user for the value of the input. This would be akin for the sin function that you'd called with for example
s = sin(pi)
to throw away your pi input and then prompt you for the angle.
Also, the proper way to test if a double variable is integer is with
if mod(variable, 1) == 0
or even better
validateattributes(variable, {'numeric'}, {'integer', 'positive', '<', '54'})
which would also check that the number is positive and smaller than 54 (for inputs greater than 53 your calculation will return incorrect results as the sum will be greater than flintmax)
And finally, the loop is pointless, the result could be calculated simply with:
output = sum(2.^(variable-1:-1:0));
which is simply equal to:
output = 2^variable - 1;
edit: so in effect, the whole function should just be:
function output = myfunc(variable)
validateattributes(variable, {'numeric'}, {'integer', 'positive', '<', '54'});
output = 2^variable - 1;
end
Thank you Guillaume , well amplified.
Guillaume, I appreciate the depth with which you have replied. However, keep in mind that I have greatly over simplified the actual function and script code. Neither is complete. I simply rewrote what code I thought necessary, in order for the question to be answered.
As for the
clear;
clc;
These I should not have put in the question. However, for the functions original purpose, they had their place. I will be using your suggestion of checking for an integer variable. Unfortunately, the code does need a FOR loop.
What I wanted to put across is the type and order of processing both the function and script need to follow.
clear never has any reason to appear in a function. The workspace of a function always starts empty except for the input variables and that workspace is always destroyed when the function terminates.
clc should also never appear in a function. Let the calling code decide what to do with the command window.
What I wanted to put across is the type and order of processing both the function and script need to follow
I'm afraid it's not very clear what you mean then. There's nothing unusual about calling a function from a script. You call your own function the same way you call matlab functions (e.g. sin). What's not right is the current design of the function you show: discard the input and prompt for a new input.
Even the code in your script doesn't make sense. I don't think you understand what the line
b = @(n) myfunc(variable)
does. and your script will error with Undefined function or variable 'b'. since b doesn't exist the first time you enter your while loop. Even if b did exist (and was a scalar numerical), your script would still error with Undefined operator '>' for input arguments of type 'function__handle' on the second iteration of the loop, since b would no longer be numerical.
Perhaps the script code should be:
boundary = input('Input: '); %what a non-descriptive prompt!
b = -inf;
n = 1;
while boundary > b
b = myfunc(n);
n = n + 1;
end
At least, it would achieve something.
Thank you. I will keep that in mind, and change what I can.
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)

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

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?
if ~exist(Out, 'var')
Out = input(....)
end

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Products

Release

R2018a

Community Treasure Hunt

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

Start Hunting!