Undefined Variable/Function hObject???
3 views (last 30 days)
Show older comments
I keep getting the an error message stating that hObject is an undefined variable or function.
How do I fix this?
Thanks in advance!
Here is the line of code and error message, callbackfn has already been defined in a separate m file.
24 callbackfn(hObject,eventdata)
Error in guieditbox (line 24)
callbackfn(hObject,eventdata)
Undefined function or variable 'hObject'.
4 Comments
Answers (1)
Guillaume
on 2 Dec 2015
Edited: Guillaume
on 2 Dec 2015
It looks to me that line 24 is actually declaring the callback function and thus should read:
function callbackfn(hobject, eventdata)
Note after your edit: My answer still stands, you're missing the function keyword. Moreover, as written, the callback will not work in a different m file. It needs to be a nested function for it have access to the variables hsttext and huitext.
2 Comments
Geoff Hayes
on 2 Dec 2015
Vic3101's answer moved here
I have changed it to
%callbackfn created as a nested function.
function callbackfn(hobject, eventdata)
nestedfx
end
But it keeps saying
Line: 24 Column: 1 Function definitions are not permitted in this context.
I'm sorry I keep asking but I've never used MATLAB before now and I'm just trying to understand what has gone wrong.
Thanks :)
Geoff Hayes
on 2 Dec 2015
Edited: Geoff Hayes
on 2 Dec 2015
If you want to nest your callback function callbackfn, then you must do so within the context of a "parent" function. So
function myMain
% stuff
function callbackfn(hObject,eventdata)
% other stuff
end
end
In the above, callbackfn is nested within the function myMain. You will have to do the same. I think that your main script is named guieditbox (or at least the file is named guieditbox.m) so just declare that as the parent function
function guieditbox
% Creating a GUI with a editable textbox for User.
% and callback function
% that prints the users text in the computer dialogue.
%Create the GUI but make it invisible
f=figure('Visible','off','color','white','Position',[360,500,625,450]);
% etc.
function callbackfn(hObject,eventdata)
% body of this callback goes here
end
end
See Also
Categories
Find more on Environment and Settings 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!