How can you use radio buttons in Guide to change a variable value in a function in the code?

2 Ansichten (letzte 30 Tage)
Me and a partner are working on a project where we are using Guide to create a graph that shows a "ball" bouncing and we have a user input different values for height and velocity as well as select an acceleration with a radio button. We have to inputs running for height and velocity but we can't seem to get the buttons to work. We have 5 buttons, each with an acceleration of a different planet, and we want the user to be able to select an acceleration that would change the graph in the Guide depending on the button pressed. The acc.handles = # is working but connecting it the equation is what were having a problem with. Also the radio buttons are already in a panel group. Thank you for your help!

Akzeptierte Antwort

Image Analyst
Image Analyst am 11 Dez. 2022
You need to set handles.acc to something in the OpeningFcn so it has a value in case you don't click a radio button.
The group selection callback looks fine - it will update the global variable handles.acc.
Then in your start button callback you need to use the current value of handles.acc:
acc = handles.acc;
and use it in your formulas as you would expect.
I'm not sure what you mean when you can't seem to get the buttons to work. Set a breakpoint at the first line in the button callback to make sure it's going into the expected callback function. Perhaps you changed the name of the Tag in GUIDE and you're not looking at the code in the actual callback function it's going to use.
  1 Kommentar
Kaitlyn
Kaitlyn am 12 Dez. 2022
Basically when we clicked the buttons which we had set for different accelerations the value wasn't changing like it was supposed to but we got it working now so thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Voss
Voss am 11 Dez. 2022
@Kaitlyn: Are the Strings of the radiobuttons really "earthRadioButton", "moonRadioButton", etc.? It seems more likely those are the Tags, and the Strings are something like, "Earth", "Moon", etc.
If that's the case, then you should use the buttons themselves to set handles.acc:
function acceleration_buttongroup_SelectionChangedFcn(hObject,eventdata,handles)
handles = guidata(hObject);
button = get(hObject,'SelectedObject');
if button == handles.earthRadioButton
handles.acc = 9.81;
elseif button == handles.moonRadioButton
handles.acc = 1.6;
% etc., other cases
end
guidata(hObject,handles);
  4 Kommentare
Kaitlyn
Kaitlyn am 13 Dez. 2022
So basically where we were entering the handels we had to use a switch case function and where we had 'SelectObject' we had to change it to 'Tag' to make those handels connect the buttons to the equation. We also needed to use the name of the tag for the case instead of the handels.

Melden Sie sich an, um zu kommentieren.

Kategorien

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

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by