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;
disp(x)
end

댓글 수: 4

Torsten
Torsten 2023년 11월 11일
편집: Torsten 2023년 11월 11일
Num = '14534535';
x = Out(Num)
x = 1
Num = '54534535';
x = Out(Num)
x = 0
% Out.m
function x = Out(Num)
x = 0;
if strncmpi(Num(1), '1',1)
x = 1;
%disp(x)
end
end
Mason
Mason 2023년 11월 11일
I still run into the same issue;
Torsten
Torsten 2023년 11월 11일
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 2023년 11월 11일
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2023년 11월 11일

0 개 추천

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);
Caution:
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.

댓글 수: 12

Mason
Mason 2023년 11월 11일
Is it possible to pass Num into Out? I cant think of any other way to verify each input of Num
result = Out(Num);
is an example of passing Num into Out
Mason
Mason 2023년 11월 11일
I feel like im doing this completely wrong... I attempted passing Num to Out and placed it where Num is defined/changed, but now its telling me Num isnt a recognized variable
Walter Roberson
Walter Roberson 2023년 11월 11일
Please show the code in which you call Out
function AddNumber(number)
% Called when number pressed
% Add number to string
set(hEdit,'string',strcat(get(hEdit,'string'),number));
drawnow;
assignin('base',"Num",number);
Press
x = Out(Num);
end
assignin('base',"Num",number);
That assigns Num in the base workspace -- which is not the current workspace. Each function has its own workspace.
Is there a reason why you are putting Num into the base workspace? Is there some other function that needs to look fot it in the basse workspace?
function AddNumber(number)
% Called when number pressed
% Add number to string
set(hEdit,'string',strcat(get(hEdit,'string'),number));
drawnow;
Num = number;
Press
x = Out(Num);
end
Mason
Mason 2023년 11월 11일
In this case, I have each uicontrol push button set to callback on the AddNumber Function. This function is supposed to act as a means to add to the "Display" on my popup menu, its purpose is to mimic what youd see on calculators, where each value is displayed in a string. The issue is that this value, when using Callback, doesnt exist after its run through the function.... and the only work around I could find was using assignin, but now im met with an additional issue where I cant assign the new value anywhere except to workspace. Im not actually sure if there was a better way to do this, but as far as I could see this was the only option I could find that was somewhat what I was hoping to achieve. As for if there was a reason for why, Im not entirely sure... I may be just overcomplicating a simple fix.
Additionally I also attempted evalin, I may just be doing this wrong but I had the same result;
%% Add number to string Function
function AddNumber(number)
% Called when number pressed
% Add number to string
set(hEdit,'string',strcat(get(hEdit,'string'),number));
drawnow;
assignin('base',"Num",number);
Press % Registers if button was pressed
Out(Num)
end
Where Num is assigned to base
function x = Out(Num)
x = evalin("base", Num(1));
disp(x)
end
function AddNumber(number)
% Called when number pressed
% Add number to string
hEdit.String{end+1} = number;
drawnow;
Num = number;
x = Out(Num);
end
function x = Out(Num)
x = 0;
if strncmp(Num(1), '1', 1)
x = 1;
end
end
Although Num is now defined and no errors arise, the display no longer works.
Do you know any possible work arounds for both to work simultaneously?
Display with previous (w/out Out Function);
%% Add number to string Function
function AddNumber(number)
% Called when number pressed
% Add number to string
set(hEdit,'string',strcat(get(hEdit,'string'),number));
drawnow;
assignin('base',"Num",number);
Press % Registers if button was pressed
end
Gives me -->
Display now;
function AddNumber(number)
% Called when number pressed
% Add number to string
hEdit.String{end+1} = number;
drawnow;
Num = number;
x = Out(Num);
end
function x = Out(Num)
x = 0;
if strncmp(Num(1), '1', 1)
x = 1;
end
end
Gives me -->
Mason
Mason 2023년 11월 12일
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
Stephen23 2023년 11월 12일
편집: Stephen23 2023년 11월 12일
@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:

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기

질문:

2023년 11월 11일

편집:

2023년 11월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by