MATLAB: Run pushbutton callback only 3 times, after that display warning message

matlab guipushbutton

Hi! I am new in matlab, can you please help me with this question: While clicking on pushbutton callback it plays wav file. I want to limit number of clicks to max 3 times. When clicking 4th time get error message: warndlg({'Warning: You exceeded number of repetitions.';}); Should I use loops or what?? Thank you in advance!!!
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if handles.status==1
handles.status=0;
play(handles.r);
end

Best Answer

in GUI initialization function:
global times_pushed
times_pushed = 0;
in your callback function:
global times_pushed
times_pushed = times_pushed + 1;
if times_pushed == 4
errordlg('your error message here');
times_pushed = 0;
end