MATLAB: GUIDE: Creating Callback in later created handle to another function

callbackguideMATLABunknown function

Hello everyone, I'm using Matlab 2007b with GUIDE and I'm creating plot handles during a button callback. For those plots (just points), I want to create a context menu that allows the user to delete a specific point. So when you right click them and click on the context option "delete", the point is deleted and some user data is removed and stored again. This works. However I'd like to also include a function execution in the callback to an already existing function another button uses. Sadly the function isn't known to that handle, or maybe I can't figure it out correctly. I'll try to illustrate this in an example:
% GUIDE functions:
function varargout = WPE_Developer(varargin)
% some stuff
%



function WPE_Developer_OpeningFcn(hObject, eventdata, handles, varargin)
% stuff etc.
%
function MY_CALLBACK(hObject, eventdata, handles, varargin)
% function which is called from a button click, x and y_matrix are example values
y_matrix = [1 2 3 4 5 6;1 2 3 4 5 6;1 2 3 4 5 6];
x = [10000, 20000, 30000];
%
for j = 1:size(y_matrix,1)
for k = 1:size(y_matrix, 2)
clear temp cmenu callbackstring;
temp = plot(x(j), y_matrix(j,k),'b.');
cmenu = uicontextmenu;
set(temp, 'uicontextmenu', cmenu);
callbackstring = [... %CALLBACK STRING
'handles = guidata(gcbf);'...
sprintf('handles.DATA.%s.%s(%d,%d) = NaN;', CarID_Name, y_get, j, k)...
...'project_changed(true, handles);'...
'guidata(gcbf,handles);'...
'delete(gco);'];
uimenu(cmenu, ...
'Label', sprintf('Delete Point (%06.0f|%06.2f)', x(j), y_matrix(j, k)), ...
'Callback', callbackstring); % THIS IS THE CALLBACK OF DELETING
end
end
%
function project_changed(true, handles)
% THIS function is unknown as a callback from the point handles above
My current code (5k lines, so I don't want to put everything here) works. Just when activating line
...'project_changed(true, handles);'...
project_changed isn't known to the point handle which I DO understand. The error is the following:
??? Undefined function or method 'project_changed' for input arguments of type 'struct'.
??? Error while evaluating uimenu Callback
Is there a way to solve this and make the function "to be known during the callback"? Splitting it into multiple .m files is not the solution I want. I found things with @-calls and some callback properties, but I don't seem to find a solution.
I'd appreciate any help, thanks.

Best Answer

If I understand correctly, you are trying to call the function project_changed through the callback function of the uimenu and that results in the error you described.
This behavior is created by the fact that the callback function is the callbackstring variable. Since this is a just a string, the call to the function is not working because the function is probably out-of-scope.
In order to make your function ‘visible’, you would need to create a separate callback function for uimenu. To do that, replace
callbackstring = [‘….]
with
callbackstring = {@uimenu_callback, CarID_Name, y_get, j,k};
where uimenu_callback is the following function:
function uimenu_callback(hObject,event,CarID_Name,y_get,j,k)
handles = guidata(gcbf);
sprintf('handles.DATA.%s.%s(%d,%d) = NaN;', CarID_Name, y_get, j, k);
project_changed(true, handles);
guidata(hObject,handles);
delete(gco);
In this function, sprintf should be change to eval in order for the string to be evaluated as a command. Also, if guidata(gcbf) does not provide you the handles, you should consider passing them as an argument to the callback.
As a side note, I would suggest the use of callback functions instead of callback 'strings', because it will enable you to perform debugging easier.
Related Question