Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
chan
chan el 23 de En. de 2018
Comentada: Jan el 26 de En. de 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!

Respuestas (1)

Jan
Jan el 23 de En. de 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 comentarios
Stephen23
Stephen23 el 25 de En. de 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 el 26 de En. de 2018
@Chan: And does my answer help you?

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