User`s guide
12 Code a Programmatic GUI
function displa y_sl ider_value(hObject,even tdata)
disp(['Slider moved to ' num2str(get(hObject,'Valu e'))]);
When you click an arrow on the slider, the output of the function looks like
this:
Slider moved to 0.01
Slider moved to 0.02
...
Both sections of code must exist in the same GUI code file. Include the
one that defines the uicontrol in a function that sets up the GUI, normally
the main function. Add the callback function as a subfunction or a nested
function. For more information, see “Subfunctions” and “Nested Functions”.
Use Cell Array Callbacks
If you need to specify arguments for a callback, you can wrap a function name
string or function handle and the arguments in a cell array.
• Identify the callback a s a string to execute a file having that nam e, for
example,
pushbutton_callback.m.
• Identify the callback as a function handle to execute a subfunction
or nested function in the currently executing code file, for example,
@pushbutton_callback.
The following two sections explore the differences between these two
approaches.
Use Cell Array s with Strings. The following cell array callback defines
a function name as a quoted string,
'pushbutton_callback',andtwo
arguments, one a variable name and one a string:
myvar = rand(20,1);
set(hb,'Callback',{'pushbutton_call back',myvar,'--m'})
Place the function name first in the cell array and specify it as a string.
When this form of callback runs, MATLAB finds and executes a file with a
.m
extension having the name of the first element in the cell array, passing it
two standard arguments followed by any additional elements of the cell array
12-14