Search code examples
matlabuser-interfacecallbackhandles

MATLAB 2013a GUI saving handles fails in checkbox callback


I'm making a GUI that reads out data from a measurement board via a serial interface. It consists of 8 channels and I want to be able to enable and disable the channels in the figure plot, but the state of my variable handles.channelsEnable isn't saved.

When opening the GUI: handles.channelEnable = [0;0;1;1;0;0;0;0]; While running the GUI I want to modify a handles.channelEnable. Inside the callback function of a checkbox it is changed, but not outside the function. I'm using guidata(hObject,handles) to save the changes made. Why aren't the changes made to handles.channelEnable saved?

    % --- Executes on button press in checkbox2.
    function checkbox2_Callback(hObject, eventdata, handles)
    % hObject    handle to checkbox2 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)

    % Hint: get(hObject,'Value') returns toggle state of checkbox2
        handles.channelEnable(2) = get(hObject,'Value');
        guidata(hObject, handles);
    end

Solution

  • You can use guidata to include a new handle in the handles variable, but whenever you want to use handles you must call guidata to obtain them. The variable handles as input variable of the callback won't have it included.