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

1 次查看(过去 30 天)
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

采纳的回答

David Sanchez
David Sanchez 2013-6-5
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

更多回答(2 个)

David Sanchez
David Sanchez 2013-6-5
Define a global variable and initialize to 0 within initialization function:
global times_pushed
times_pushed = 0;
Within the callback function, increment the value of this variable each time the function is called ( the button in pushed )
timer_pushed = times_pushed + 1;
if times_pushed == 4
errordlg('your error message here');
times_pushed = 0;
end
  2 个评论
Iain
Iain 2013-6-5
Don't use globals if you can avoid them.
You can use "persistent" variables, if you only want one function to access it.
You can put a count of how many times it has been played into the "userdata" property of a graphics object and retrieve it with set and get

请先登录,再进行评论。


Lucky
Lucky 2013-6-5
Thank you very much! But i still dont understand where should i put this: if handles.status==1 handles.status=0; play(handles.r); end
Should it be after initializing the variable?
global times_pushed
times_pushed = 0;
if handles.status==1
handles.status=0;
play(handles.r);
end
times_pushed = times_pushed + 1;
if times_pushed == 4
errordlg('your error message here');
times_pushed = 0;
end
  1 个评论
Iain
Iain 2013-6-5
set(h, 'Userdata', variable)
Takes the contents ot "variable", and puts it in the "userdata" property of the item referred to by "h".
v = get(h,'Userdata')
Retrives the contents of the userdata property of the item referred to by h, and puts it in the variable "v".

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by