Matlab update popmenu string

Hi,
I have a gui wich has a lot of menus associated with radiobuttons. Menus or popmenus will be available to edit or select only if the radiobuttons next to them is selected.
Among these menus i have 2 popmenus associated to a radiobutton. In the first popmenu i have set up in the string 3 different choices and if i take choice 1 and 2 the second popmenu will propose a string with 5 choice. But if i select choice 3 in the second popmenu i have only it will change the string of the popmenu 2 and all the proposition will be different.
To do that i have a function which works but the only problem is that to see the change of string in the popmenu 2 i need to unselect and select again the radiobutton. Is there a way to refresh the popmenu 2 choices when i change the selection in popmenu 1?
Here is the function that allow to change the choices in the popmenu 2 :
function Scr519(h)%h contains the two popmenus
enable(h)%Enable both popmenus
x=get(h(2),'Value');%get the selected element in the popmenu2
switch get(h(1),'Value')% get the selected element in the popmenu1
case {1,2}% The selected element in popmenu1 is the first or second in order
if (x<1)||(x>5),x=1;end %Set the selected element of the popmenu2 to the first element.
ch='Iscr<20|20<Iscr<50|50<Iscr<100|100<Iscr<1k|1000<Iscr';%ch is the popmenu2 string
case 3
if (x<1)||(x>3),x=1;end
ch='Iscr<25|25<Iscr>50|Iscr>=50';
otherwise
error(kverranorm(6))
end
set(h(2),'String',ch,'Value',x)%Update the string of the popmenu2 and it's selected element
I put in an exemple file containing the gui and the function (until line 179 it's the gui then we have the function that enable/disable the menus when the radiobutton next to them is selected, then we have this function)

6 Comments

It sounds like you understand exactly what you want to do. However, that is not at all clear to me.
The allowed options in some of your dropdowns change depending on other settings. Why don't you write a function that updates your dropdown? Then you can call that function in every callback of selectors that affect it. That function then provides you with a single place to determine what the options should be.
I mean something like the structure below. You should also really try to write more comments. The code included in your post is very hard to understand
function callback_checkbox(hObject,eventdata)
% do the things related to the checkbox
___
% update the other dropdown
update_dropdown_B(guidata(hObject))
end
function callback_dropdown_A(hObject,eventdata)
% do the things related to one dropdown
___
% update the other dropdown
update_dropdown_B(guidata(hObject))
end
function update_dropdown_B(h)
% determine the options based on the other objects
AllowedOptions = ___
% set those options here
set(h.dropdown_B,'String',AllowedOptions)
end
I will try your suggestion thx. I will try to explain how the function work : I have a first popmenu form which i can select 3 option (choice 1,choice2 and choic3) my function will check which option i selected and will proceed in two ways :
If it find that i selected choice 1 or choice 2. Then my second popmenu will allow me to select from 5 options let's say 1,2,3,4 or 5.
But if my fonction find that i selected choice 3. Then my second popmenu will allow me to select from 3 option different from 5 options seen above.
That's what my function does but if i go from choice 1 to choice 3 or choice 3 to choice 2 my second popmenu won't update and that's what i want to get to.
I hope you can understant it better now.
I don't really see how your code is expected to do what you're describing. However, I expect you will be able to fill in the blanks in the code I posted so you should be able to solve your own problem.
@Rik you're solution gives me the same result to update the string of the second popmenu i still need to leave unselect/select the radiobutton. To me i think i need to use a loop. I tried using a while with condition that the radiobutton value is equal to 1 but if i do that my whole gui crashes.
function Scr519(h,ra)%h contains the two popmenus and ra contains the radiobuttons
enable(h)%Enable both popmenus
while get(findobj(ra,'Tag','Norme519'),'Value') == 1
x=get(h(2),'Value');%get the selected element in the popmenu2
x1=get(h(1),'Value')% get the selected element in the popmenu1
if x1==1 || x1==2
if (x<1)||(x>5),x=1;end
ch='Iscr<20|20<Iscr<50|50<Iscr<100|100<Iscr<1k|1000<Iscr';
elseif x1 ==3
if (x<1)||(x>2),x=1;end
ch='Iscr<50|Iscr>=50';
else
error(kverranorm(6))
end
set(h(2),'String',ch,'Value',x)%Update the string of the popmenu2 and it's selected element
end
it doesn't give me an error code but it's stuck on the line x1=get(h(1),'Value').
Why do you think you need to use a while loop? What do you mean by being stuck?
I'm used to using h to contain the guidata struct, so seeing it being used as an array of handles is a bit confusing.
What you need to do is gather all relevant settings and determine what options should exist in your second dropdown and which should be currently sellected. Your current code does that.
Perhaps you should put a breakpoint at the beginning of your function and step through the code line by line.
Your comments are already quite an improvement, although I would suggest putting them on separate lines. Your initial goal should be to have about half of the characters in a function to be documentation. That is probably too strict, but if you aim for 50%, you'll end up at a more reasonable ratio. Remember that you need to understand what you did in 6 months as well. I have absolutely no clue what I did in my code before I started rigorous commenting. From that point onward I understand what I did, even if I haven't looked at the code for years.
ok thank you for your advise i will do that.
And by being stuck i mean that it will enable the 2 popmenus but after that it will just constantly return the value of x1 = 1 and do nothing else.

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2012a

Asked:

Ali
on 12 Oct 2022

Commented:

Ali
on 13 Oct 2022

Community Treasure Hunt

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

Start Hunting!