MATLAB Creating Graphical User Interfaces

(ff) #1

% maxval = get(hObject,'Max');
% data.diffMax = maxval - data.val;


guidata(hObject,data);
end


function button_callback(hObject,eventdata)
data = guidata(hObject);
display([data.val data.diffMax]);
end


When the user moves the slider, the slider_callback function executes these
commands to retrieve and modify the stored data:



  • data = guidata(hObject) retrieves the stored data as a structure.

  • data.diffMax = maxval - data.val modifies the diffMax field in the structure.

  • guidata(hObject,data) stores the modified structure.


When the user clicks the push button, the button_callback function calls guidata to
retrieve a copy of the stored structure. Then it displays the two values stored in the
structure.


Use guidata in GUIDE Apps


GUIDE uses the guidata function to store a structure called handles, which contains all
the UI components. MATLAB passes the handles array to every callback function. If you
want to use guidata to share additional data, then add fields to the handles structure
in the opening function. The opening function is a function defined near the top of your
code file that has _OpeningFcn in the name.


To modify your data in a callback function, modify the handles structure, and then store
it using the guidata function. This slider callback function shows how to modify and
store the handles structure in a GUIDE callback function.


function slider1_Callback(hObject, eventdata,handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)


% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range
handles.myvalue = 2;


Share Data Among Callbacks
Free download pdf