MATLAB: H i every one , i need to declare a counter such that if any one of the four buttons is pressed a specefic funtion specified in the fifth button is performed.Suppose UserData can not be used it reserved for something else

gui

function pushbutton1_Callback(hObject, eventdata, handles) counter=1;
function pushbutton2_Callback(hObject, eventdata, handles)
counter=2;
function pushbutton3_Callback(hObject, eventdata, handles) counter=3;
function pushbutton4_Callback(hObject, eventdata, handles)
counter=4;
% — Executes on button press in pushbutton5. function pushbutton5_Callback(hObject, eventdata, handles)
if counter==1 % % elseif counter==2 % elseif counter==3 % elseif counter==4
end

Best Answer

  • Use guidata, which calls setappdata and getappdata internally:
    function pushbutton1_Callback(hObject, eventdata, handles)
    handles = guidata(hObject);
    handles.counter = 1;
    guidata(hObject, handles);
    % And the same for the other callbacks...
    function pushbutton5_Callback(hObject, eventdata, handles)
    handles = guidata(hObject);
    switch handles.counter
    case 1
    case 2
    case 3
    case 4
    otherwise % No SWITCH without OTHERWISE
    error('Programming error')
    end
    end