How to create a minus counter

on the code above i created a add counter
function patient_Callback(hObject, eventdata, handles)
counter2 = get(hObject, 'UserData') + 1;
set(hObject, 'UserData', counter2);
set(handles.Impt, 'String', sprintf('Number of Patients currently: %d\n', counter2));
but the minus code doesnt work
function patients_Callback(hObject, eventdata, handles)
counter2 = get(hObject, 'UserData') - 1;
set(hObject, 'UserData', counter2);
set(handles.Impt, 'String', sprintf('Number of Patients currently: %d\n', counter2));
guidata(hObject , handles);
What should i change to the minus code.

10 commentaires

Walter Roberson
Walter Roberson le 12 Mar 2018
What difficulty do you observe?
The guidata call is not necessary there but should not hurt.
Kwan Zhao Jie
Kwan Zhao Jie le 12 Mar 2018
the counter does not minus 1 when i press the button. The adding works fine.
Walter Roberson
Walter Roberson le 12 Mar 2018
Is it certain that the function is the callback for a uicontrol with tag 'patients'? If you put in a breakpoint is the function run at all?
Kwan Zhao Jie
Kwan Zhao Jie le 12 Mar 2018
Yes the tag i placed for the second button is patients. if i placed end at the end of the function, it doesn't run.
Walter Roberson
Walter Roberson le 12 Mar 2018
Have you tried using the debugger to see if the function is being called?
Kwan Zhao Jie
Kwan Zhao Jie le 12 Mar 2018
Modifié(e) : Kwan Zhao Jie le 12 Mar 2018
the function was not called. only the function for the addition was called. the function for addition is nurse and patient. Attached is the results.
Walter Roberson
Walter Roberson le 12 Mar 2018
Please attach your code and the fig file.
Kwan Zhao Jie
Kwan Zhao Jie le 13 Mar 2018
Attached is the Fig file and the code. Thank you in advance!
Impt_CreateFcn is missing.
For both patient_Callback and patients_Callback, you use
counter2 = get(hObject, 'UserData')
but notice that for patient_Callback, hObject is data attached to handles.patient, and for patients_Callback, hObject is data attached to handles.patients . You are not using the same data.
You should change patients_Callback to access handles.patient rather than hObject .
Kwan Zhao Jie
Kwan Zhao Jie le 13 Mar 2018
Thanks! It works now!

Connectez-vous pour commenter.

Réponses (1)

Santosh Prasad Choudhury
Santosh Prasad Choudhury le 12 Mar 2018

0 votes

Please try to check the user data deafult value and how you validate them for value less than 0? please let me know

Catégories

En savoir plus sur Data Distribution Plots dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by