Specify Callbacks in Function Calls matlab

2738 단어
Specify a component callback property value as one of the following:
  • A string that contains one or more MATLAB ortoolbox commands to evaluate
  • A handle to a function that is within scope when theGUI is running
  • A cell array containing a string function name ora function handle, plus optional strings, constants, or variable namesfor arguments

  • Use String Callbacks


    You can concatenate commands in a string callback. This one,for example, adds a title to the plot it creates.
    set(hb,'Callback',...
           'plot(myvar,''--m''); title(''String Callback'')')

    Use Function Handle Callbacks


    Here is an example of declaring a callback when defininga uicontrol:
    figure
    uicontrol('Style','slider','Callback',@display_slider_value)

    Hereis the definition of the function in the GUI code file. The callbackprints the value of the slider when you adjust it:
    function display_slider_value(hObject,eventdata)
    disp(['Slider moved to ' num2str(get(hObject,'Value'))]);

    Whenyou click an arrow on the slider, the output of the function lookslike this:
    Slider moved to 0.01
    Slider moved to 0.02

    Use Cell Arrays with Strings.
     The following cellarray callback defines a function name as a quoted string, 'pushbutton_callback',and two arguments, one a variable name and one a string:
    myvar = rand(20,1);
    set(hb,'Callback',{'pushbutton_callback',myvar,'--m'})

    Thesetwo arguments are followed by whatever arguments you include whenyou specify the callback for the component. Code to execute 'pushbutton_callback' mightlook like this:
    function pushbutton_callback(hObject, eventdata, var1, var2)
    plot(var1,var2)

    Use Cell Arrays with Function Handles.  
    You canspecify a callback function using a function handle insteadof using a function name.
    The following variation uses afunction handle to specify pushbutton_callback asthe callback routine to be executed when a user clicks Plotline.
    figure;
    hb = uicontrol('Style','pushbutton',...
                   'String','Plot line')
    set(hb,'Callback',{@pushbutton_callback,myvar,'--m'})

    As above, the callback is in a file named pushbutton_callback.m,which contains code such as this:
    function pushbutton_callback(hObject, eventdata, var1, var2)
    plot(var1,var2)

    좋은 웹페이지 즐겨찾기