Effacer les filtres
Effacer les filtres

how to created and get data for use several action in pushbutton GUI?

2 vues (au cours des 30 derniers jours)
chan
chan le 23 Jan 2018
Commenté : Jan le 26 Jan 2018
%Sample code
function a_input_Callback(hObject, eventdata, handles)
input = str2num(get(hObject,'String'));
if (isempty(input))
set(hObject,'String','0')
end
guidata(hObject, handles);
function a_input_Callback(hObject, eventdata, handles)
input = str2num(get(hObject,'String'));
if (isempty(input))
set(hObject,'String','0')
end
guidata(hObject, handles);
%for example I have push button
function add_push_Callback(hObject, eventdata, handles)
a = get(handles.a_input,'String');
b = get(handles.b_input,'String');
total = str2num(a) + str2num(b);
c = num2str(total);
set(handles.answer,'String',c);
guidata(hObject, handles);
function div_push_Callback(hObject, eventdata, handles)
a = get(handles.a_input,'String');
b = get(handles.b_input,'String');
total = str2num(a) / str2num(b);
c = num2str(total);
set(handles.answer,'String',c);
guidata(hObject, handles);
% What I want to as is, I dont want to create a = get(handles.a_input,'String');
% and b = get(handles.b_input,'String'); again and again in each button action
%just want to create it only one time and than we can call it to do use in each button.
Thanks!

Réponses (1)

Jan
Jan le 23 Jan 2018
You have 2 "a_input_Callback" in your posted code.
You can use the same callback with different input arguments, if you want to run the same code:
function op_push_Callback(hObject, eventdata, handles, operator)
a = str2num(get(handles.a_input, 'String'));
b = str2num(get(handles.b_input, 'String'));
switch operator
case '+'
r = a + b;
case '/'
r = a / b;
otherwise
error('Bad operator: %s', operator);
end
c = num2str(total);
set(handles.answer, 'String', c);
end
Now you can define the callbacks accordingly adding the wanted operator. I'm not sure how this works in GUIDE, but it must be easy to define it as additional argument.
If handles was not changed in a callback, guidata(hObject, handles); is not needed.
  2 commentaires
Stephen23
Stephen23 le 25 Jan 2018
chan's "Answer" moved here:
oh, I am Confuse have only 1 a_input_Callback and 1 b_input_Callback, not 2 a_input_Callback
Jan
Jan le 26 Jan 2018
@Chan: And does my answer help you?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by