I have this line of code:
delete_btn = uicontrol(rr_ops, 'Style', 'pushbutton', 'String', 'Delete Graphic', 'Position', [13 135 98 20], ...
'Callback', 'delete_graphic');
and a little bit upper this function:
function delete_graphic
global rr_list
selected = get(rr_list, 'Value');
selected
return;
why this code is not working? I really dont understand…
What do I need? I create one button and a listbox, clicking on button – deleting selected element from a listbox.
Thx for help.
PS
Always getting this error:
??? Undefined function or variable 'delete_graphic'.
??? Error while evaluating uicontrol Callback
here is all my code: http://paste.ubuntu.com/540094/ (line 185)
The generally-preferred way to define a callback function is to use a function handle instead of a string. When you use a string, the code in the string is evaluated in the base workspace. This means that all the variables and functions used in the string have to exist in the base workspace when the callback is evaluated. This makes for a poor GUI design, since you don’t really want the operation of your GUI dependent on the base workspace (which the user can modify easily, thus potentially breaking your GUI).
This also explains the error you are getting. The function
delete_graphicis defined as a subfunction in your filerr_intervals.m. Subfunctions can only be called by other functions defined in the same m-file, sodelete_graphicis not visible in the base workspace (where your string callback is evaluated). Using a function handle callback is a better alternative. Here’s how you would do it:'delete_graphic'to@delete_graphic.Change the function definition of
delete_graphic(line 185) to:where
hObjectis the handle of the object issuing the callback andeventdatais optional data provided when the callback is issued.EDIT:
If you want to pass other arguments to
delete_graphic, you can perform the following steps:Add the additional input arguments to the end of the function definition. For example:
Use a cell array when you set the callback for your button, where the first cell contains the function handle and the subsequent cells each contain an input argument. For example:
There is one caveat to this, which is that the values
AandBstored in the cell array are fixed at what they are when you set the callback. If you changeAorBin your code it will not change the values stored in the cell-array callback.If you aren’t able to use the above solution (i.e. if
AandBneed to change value), there are a few other options for how you can share data among a GUI’s callbacks:UserDataproperty of a uicontrol object. To access or update it, you just need the object handle.handlesstructure GUIDE creates to store data using the GUIDATA function.