Clear Filters
Clear Filters

Not Enough Input Arguments

34 views (last 30 days)
Mason on 11 Nov 2023
Edited: Stephen23 on 12 Nov 2023
I've only been using Matlab for around 2 months and Im trying to make a function that recognizes new inputs. I have "Num" defined in another function as a character array, everytime "Num" is changed I want to run it through this function. The issue is that when I attempt to test the function it recognizes the input has changed, but stops me at "Not Enough Input Arguments" Its a relatively simple function, and whenever I attempt to alter it I get "Unrecognized function or variable 'Num'." instead. I just want an Idea of how I should go abou this.
Here is the test function;
In this case I wanted to define x as 1 if strncmpi(Num(1), '1',1) was true, but I dont know where to proceed. I also wanted to add that when I try this function in command window alone it works just fine and udates x as a new variable.
% Out.m
function x = Out(Num)
if strncmpi(Num(1), '1',1)
x = 1;
Torsten on 11 Nov 2023
I don't see that you call the function somewhere with an input argument for "Num". What do you expect as output if you don't supply input ?
Walter Roberson
Walter Roberson on 11 Nov 2023
If you have a named parameter Num and you do not pass in a value in the corresponding position, and you have a variable named Num in your base workspace (such as a script)... then MATLAB will never look in the base workspace to see if you have Num defined there. never .
In every case when you have a named parameter to a function, and you do not pass a value in the corresponding position in the function call, then it is an error to try to use the value of that variable name (unless, that is, your code already assigned a value to the variable.)
This is a strict rule. Named parameters are never searched for outside of the function they are a named parameter of.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 11 Nov 2023
I have "Num" defined in another function as a character array,
But you are not passing Num into Out when you invoke Out.
There are absolutely no circumstances under which MATLAB will try to look outside of the current function to try to find a definition for a variable that is named as an input parameter to the current function.
If you do not pass a value as the first parameter to Out then MATLAB will never look in the calling script or function to determine whether a variable named Num happens to be defined there.
(There are limited circumstances under which MATLAB will search for variables in "outer" functions, but those circumstances never include variables defined in the function definition of the function that needs the variable.)
The place that invokes Out needs to call something like
result = Out(Num);
Your code does not define any value for x in the case that Num(1) is not '1'
Note by the way strncmpi() is a case-INsensitive character vector comparison. It is useful, for example, in testing whether whether an input is either 'a' or 'A' . But there is no upper-case or lower-case version of the digit '1' so it is pointless to do a case-INsensitive test.
Your code will fail if the input Num is an empty vector. Interestingly, your code will not (directly) fail if the input is the scalar string "" -- in that case Num(1) would be the scalar string "" and strncmpi("", '1', 1) would be a valid test.
Mason on 12 Nov 2023
I found a solution using both my original code, and your new one.
Even though the values arent displayed, because they are nested in the function ready to use rather than zapped into the void after the function ends, I can use them aslong as I keep the funciton within my loop.
For now, this is helps me do what I need to do, I just hope this doesnt end up bitting me in the end.
Thank you for the help, I think I understand how to tackle this now.
Stephen23 on 12 Nov 2023
Edited: Stephen23 on 12 Nov 2023
@Mason: when sharing data between callbacks you basically have a few choices:
  1. store the data within the GUI objects (this is what GUIDE's handles data and APP's properties do)
  2. use a shared workspace (i.e. nested functions).
  3. SAVE & LOAD (might be useful in some cirumstances)
  4. magically making data appear in other workspaces (e.g. EVALIN, ASSIGNIN etc... best avoided)
If you are writing your own GUI then nested functions are a very good approach. See also:

Sign in to comment.

More Answers (0)


Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!