Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
chan
chan am 23 Jan. 2018
Kommentiert: Jan am 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!

Antworten (1)

Jan
Jan am 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 Kommentare
Stephen23
Stephen23 am 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 am 26 Jan. 2018
@Chan: And does my answer help you?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by