Using Radio Buttons with Push Button

3 ビュー (過去 30 日間)
Kaan Uçar
Kaan Uçar 2019 年 5 月 17 日
コメント済み: Dennis 2019 年 5 月 17 日
Hey everybody. I have a homework which asks me to create GUI which will allow user to enter two numbers, then choose an operation (addition, substraction, multiplication, division) by using radio buttons, and then will give the result when they click the push button.
Here's what I've tried. I tried to contain all the codes in Pus Button Callback. It does not work: ("sonuc" is the result of the operation. It is the tag of my static text box.)
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
x = get(handles.sayi_1, 'String');
x = str2num(x);
y = get(handles.sayi_2, 'String');
y = str2num(y);
switch get(eventdata.uibuttongroup1, 'Tag')
case t
islem = x + y;
set(handles.sonuc, 'String', islem)
case c
islem = x - y;
set(handles.sonuc, 'String', islem)
case cc
islem = x * y;
set(handles.sonuc, 'String', islem)
case b
islem = x / y;
set(handles.sonuc, 'String', islem)
end

回答 (1 件)

Dennis
Dennis 2019 年 5 月 17 日
Try changing
switch get(eventdata.uibuttongroup1, 'Tag')
to
switch get(handles.uibuttongroup1.SelectedObject)
And you need to change t, c, cc and b to the correct handles of your radiobuttons (probably handles.radiobutton1, handles.radiobutton2 ....)
  2 件のコメント
Kaan Uçar
Kaan Uçar 2019 年 5 月 17 日
I tried what you said, but when I execute the operation it tells me the switch input must be a scalar or a string.
My switch cases look someting like this:
case handles.t
Dennis
Dennis 2019 年 5 月 17 日
My bad, it needs to be
switch get(handles.uibuttongroup1,'SelectedObject')

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by