GUI callbacks now broken
Show older comments
I've been making some changes to a GUI and I broke a lot of my callbacks in the process. Specifically, the callbacks that are now broken are all saved in separate m-files outside of my main GUI file. The only changes I made were to the names of the callback functions themselves. I changed the names in the function signatures in GUIDE (inside Property Inspector) and I changed the names in the associated m-files and now none of the changed callbacks work. To be sure, nothing happens when I press any of the buttons associated these callbacks. Moreover, if I hit the buttons multiple times, a second instance of my GUI is loaded indicating that the opening function was executed again.
Do I need to change something else? All of the callbacks worked fine in separate files to begin with, but now they don't.
I should also note that if I copy and paste the callbacks back into the main GUI file, then the callbacks work again.
EDIT 1:
If I press one of the buttons with a broken callback a few times then I get the following error.
Error using handle.handle/get
Invalid or deleted object.
Error in movegui>getActivePositionProperty (line 235)
actPosProp = get(fig,'ActivePositionProperty');
Error in movegui (line 115)
oldposmode = getActivePositionProperty(fig);
Error in openfig (line 109)
movegui(fig(n), 'onscreen');
Error in gui_mainfcn>local_openfig (line 286)
gui_hFigure = openfig(name, singleton, visible);
Error in gui_mainfcn (line 234)
gui_hFigure = local_openfig(gui_State.gui_Name, 'reuse',gui_Visible);
Error in main_gui (line 23)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)main_gui('ConnectSystem',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
The movegui error is kind of odd.
3 Comments
Walter Roberson
on 29 Jun 2012
The directory that your .m files are in: is it on your MATLAB path specifically? If you are relying on the files being found in the current directory, then are you possibly cd()'ing to a different directory along the way (such as when you load data files for the user) ?
tlawren
on 29 Jun 2012
Walter Roberson
on 29 Jun 2012
load() itself will not change directories.
Answers (1)
Image Analyst
on 30 Jun 2012
I really recommend you don't alter the callback names that GUIDE picks. If you want custom names, just call that custom-named function from within the callback. For example in the callback for btnSelectFolder, let it use the name btnSelectFolder_Callback which it wants to, then inside that function call your function, like this:
function btnSelectFolder_Callback(hObject, eventdata, handles)
% hObject handle to btnSelectFolder (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
fprintf(1, 'Entered btnSelectFolder_Callback');
% Now call your function with your special name.
usersFolder = MyCustomFolderSelectionFunction();
fprintf(1, 'Leaving btnSelectFolder_Callback');
return;
Fortunately, you can fix whatever you messed up. In the property inspector for your control (e.g. your button), there is a property called Callback. Click in there and delete everything, then click the little icon to the left and it will fix it by putting in the automatic, default name like it started out with before you mucked it up.
If you're having your callbacks, the same ones in the callback property of GUIDE, be in separate m-files, then I doubt that will work, just like you're observing. How would it know to call those? It's going to look only in the m-file associated with your fig file, not all over the search path. And if it doesn't find it there, it will complain. That's why I said to use the original names and simply call your other m-file from within the callback, as I demonstrated above.
3 Comments
Walter Roberson
on 30 Jun 2012
Edited: Walter Roberson
on 30 Jun 2012
GUIDE codes callbacks as strings that represent code. When MATLAB sees a string as the callback then MATLAB feval()'s the string in the context of the base workspace. Each of the callback strings that GUIDE creates represents code that fetches the current handles data structure, and then calls the specific callback routine by name. Remember, that is executing in the base workspace, so if the routine name is known anywhere in the current MATLAB path, it will be able to find the routine and execute it. It very definitely does not only look in the .m file associated with the .fig file.
Setting a callback property to a string causes MATLAB to evaluate that string in the base workspace when the callback is invoked.
My guess is that when GUIDE noticed that there were no routines in the .m file named with the expected callback routine names, it created new empty routines, and it is those routines being called, thus giving the behavior of "nothing happens" (with no error).
Walter Roberson
on 2 Jul 2012
Time to use the debugger and check the Callback properties
Categories
Find more on Adding custom doc 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!