Undefined Variable/Function hObject???
Mostra commenti meno recenti
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 Commenti
Geoff Hayes
il 1 Dic 2015
Vic3101 - the error message is telling you that the variable hObject is undefined (i.e. has not been declared prior to your line of code trying to use it). What can you tell us about the function (gui?) guieditbox as it is line 24
callbackfn(hObject,eventdata)
that is generating the error message. If you put a breakpoint at this line and then launch your GUI, what can you say about hObject and eventdata when the debugger pauses at this line? Where are they initialized (or passed in) prior to calling callbackfn?
Please provide more
Walter Roberson
il 1 Dic 2015
We need to see the entire function that includes that line.
Vic3101
il 2 Dic 2015
Risposte (1)
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 Commenti
Geoff Hayes
il 2 Dic 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
il 2 Dic 2015
Modificato: Geoff Hayes
il 2 Dic 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
Categorie
Scopri di più su Just for fun in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!