Undefined Variable/Function hObject???

3 views (last 30 days)
Vic3101
Vic3101 on 1 Dec 2015
Edited: Geoff Hayes on 2 Dec 2015
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
Vic3101
Vic3101 on 2 Dec 2015
Edited: Guillaume on 2 Dec 2015
Here is the code for the entire GUI I'm trying to create, its basically a screen where players for a game enter their name and then when they press enter the dialogue changes on the GUI from "Hello there, what is your name" to "Hello John would you like to play noughts and crosses?"
Its a piece of code I have edited from a MATLAB textbook example which creates a simple editable textbox GUI. The function callbackfn(hObject,eventdata) was in the textbook script and I havent changed that at all but I have defined it as a function in another m file however its stating its undefined still. As far as the function of the GUI goes I can type my name in the textbox but the enter button when pushed doesnt work or do anything. Here is the remainder of the script with comments.
% 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])
% Putting a name on it and moving it to center.
set(f,'Name','GUI with editable text')
movegui(f,'center')
% Creating two objects: a box where the user can type.
% Edit a string and text title for editable box.
hsttext=uicontrol('Style','text','BackgroundColor','white','Position',[80,300,450,80],'String','Hello there, What is your name?','ForegroundColor','Black','FontSize',30);
huitext=uicontrol('Style','edit','Position',[90,200,400,40],'ForegroundColor','Black','FontSize',30,'CallBack',@callbackfn);
% Now the GUI is made visible.
set(f,'Visible','on')
%callbackfn created in separate m file.
callbackfn(hObject,eventdata)
set([hsttext huitext],'Visible','off');
% Get user string input and print
% In big red letters
printstr=get(huitext,'String');
hstr=uicontrol('Style','text','BackgroundColor','white','Position',[100,425,400,55],'String',printstr,'ForegroundColor','Black','FontSize',30);
set(hstr,'Visible','on')
Thank you for replying so quick!
Vic3101
Vic3101 on 2 Dec 2015
Also when I defined the function in another m.file as
function callbackfn(hObject,eventdata)
When I run this alone not in the context of the GUI there is no error message.

Sign in to comment.

Answers (1)

Guillaume
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
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
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

Sign in to comment.

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!