Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
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

Respuesta aceptada

David Sanchez
David Sanchez el 5 de Jun. de 2013
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

Más respuestas (2)

David Sanchez
David Sanchez el 5 de Jun. de 2013
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 comentarios
Iain
Iain el 5 de Jun. de 2013
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 el 5 de Jun. de 2013
thank you!! will try this option as well!

Iniciar sesión para comentar.


Lucky
Lucky el 5 de Jun. de 2013
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 comentario
Iain
Iain el 5 de Jun. de 2013
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".

Iniciar sesión para comentar.

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by