problem with radiobuttons and If loop in GUI

Hello all,
I'm workin on a project with GUI but I'm not well experienced. I have different radiobuttons in 2 pannels and depending on the user selection, a certain loop is executed when he clicks a pushbutton.
Here is a sample of what the code:
function uipanel2_SelectionChangeFcn(hObject, eventdata, handles) global exchanger_selection
if hObject ==handles.radiobutton1
exchanger_selection=1;
elseif hObject ==handles.radiobutton2
exchanger_selection=2;
elseif hObject ==handles.radiobutton3
exchanger_selection=3;
end
.
function uipanel7_SelectionChangeFcn(hObject, eventdata, handles) global design_selection
if hObject ==handles.radiobutton6
design_selection=1;
elseif hObject ==handles.radiobutton7
design_selection=2;
end
.
function pushbutton9 _Callback(hObject, eventdata, handles) global exchanger_selection _selection
if all(all(exchanger_selection==1) && all(design_selection==1))
...end
elseif all(all(exchanger_selection==1) && all(design_selection==2))
...end
elseif all(all(exchanger_selection==2) && all(design_selection==1))
...end
elseif all(all(exchanger_selection==2) && all(design_selection==2))
...end
Now inside each if statement I have a lengthy loop includes for loops and if statements. The problem is when I click on certain group of radio buttons then the push button, GUI doesn't excute the right loop, instead it always executes the last loop skipping the conditions.
In the if condition statements I have also tried using & with 'all' and without 'all', I used isequal and I tried switch case but the problem still the same.
I would be thankful if someone can help me figure out the source of the problem.
Thanks in advanced.

 Accepted Answer

Is exchanger_selection actually updating as you expect when you put a breakpoint in your pushbutton9 callback?
It really isn't advisable to use global variables though.
if hObject ==handles.radiobutton1
handles.exchanger_selection=1;
elseif hObject ==handles.radiobutton2
handles.exchanger_selection=2;
elseif hObject ==handles.radiobutton3
handles.exchanger_selection=3;
end
guidata( hObject, handles )
---------------------
function pushbutton9 _Callback(hObject, eventdata, handles)
if handles.exchanger_selection == 1 && handles.design_selection == 1
etc
would be more advisable.
Also you really don't need those all's when your variables in question are just scalars. They don't do any harm if you get the statement logically correct, but you want the simplest statement of logic you can get to have the least chance of bugs.

6 Comments

Thank you Adam for the useful answer,
I tried what you advised, now the loops are being executed correctly for two selections only, but for the other two, these are:
if all(all(exchanger_selection==1) && all(design_selection==1))
...end
elseif all(all(exchanger_selection==2) && all(design_selection==1))
...end
I got an error messege says:
Reference to non-existent field 'design_selection'.
Error in ==> GUI29>pushbutton9_Callback at 1511
if handles.exchanger_selection==2 && handles.design_selection==1
Do you have any idea what can be the reason behind this?
Thank you very much.
If you literally just followed what I put above you need to also do the same thing for design_selection instead of using a global variable. I didn't include that in my example as it should work exactly the same as exchanger_selection in my example. i.e.
if hObject ==handles.radiobutton6
handles.design_selection=1;
elseif hObject ==handles.radiobutton7
handles.design_selection=2;
end
guidata( hObject, handles )
I already did this for the design selection and removed the global variable but I got the error above.
Is there any other statement I should make sure to include somewhere in the pushbutton callback?
Thanks again.
Did you remember to add the
guidata( hObject, handles )
line after your code for design_selection?
Also what you will need to do is initialise these in the OpeningFcn as e.g.
handles.design_selection = 1;
Basically whatever you default your radio buttons to when you open the GUI you should set these variables to because they need to work in the case where you do not click the radio button because the default is what you want.
In the above case if you never click the radio button the handles.design_selection field will never get created.
Adam, it worked when I set the default in the OpeningFcn. Thank you very much for your great help, really appreciated.
No problem :)

Sign in to comment.

More Answers (0)

Categories

Asked:

on 14 Nov 2014

Commented:

on 15 Nov 2014

Community Treasure Hunt

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

Start Hunting!