How to disable an option when i choose the same option at the other me

5 views (last 30 days)
Hi guys, i need help with something:
I'm working on GUIDE with a conversor program, that program convert any value inserted to any base (decimal, binary, etc)
My problem is the next:
When i choose an option in the first popupmenu the same option doesn't have to appear in the popupmenu 2.
How i can do that?
For example i put a numer, and i choose in what base it is (binary) and i wanna on the other popup menu this option doesn't show (binary).
Here i attached a pic:
1.png 2.png
And this is my code:
value_in_str = handles.edit1.String;
switch handles.popupmenu1.Value
case 1
msgbox('No ha seleccionado ninguna base a convertir');
case 2
value_in_dec = bin2dec(value_in_str);
case 3
value_in_dec = hex2dec(value_in_str);
case 4
value_in_dec = sscanf(value_in_str,'%lo')
case 5
value_in_dec = sscanf(value_in_str,'%ld');
end
switch handles.popupmenu2.Value
case 1
msgbox('No ha seleccionado ninguna base a convertir');
case 2
value_out_str = dec2bin(value_in_dec);
case 3
value_out_str = dec2hex(value_in_dec);
case 4
value_out_str = sprintf('%lo',value_in_dec)
case 5
value_out_str = sprintf('%ld',value_in_dec);
end
handles.edit2.String = value_out_str;
  7 Comments
Walter Roberson
Walter Roberson on 1 Oct 2019
Which MATLAB release are you using? None of your posts seems to indicate that.
If you command
dbstop if error
and run your code, then what shows up for class(handles.popupmenu1) ?

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 27 Sep 2019
Edited: Adam Danz on 27 Sep 2019
The details will vary depending on how your GUI is created (GUIDE, appdesigner, or uicontrol) but the logic will be the same. A callback function will be assigned to the first popup menu and the handles passed to that callback menu should include a master list of options for the popup menus. The callback function should achieve 4 things:
  1. Identify the current selection in each menu
  2. Temporarily remove the item selected in menu #1 from the master list
  3. Assign the altered master list to menu #2 (missing the item selected from menu1)
  4. Return the original section to menu2 if it still exists.
Here's a functional demo that creates the 2 popup menus and the callback function. You can adapt it to your needs.
% Create the GUI with 2 popup menus
% Store all handles in 'h'
f = figure();
h.ListOptions = {'dog','cat','mouse','rat','pig'}; % Master list of options
% Create menu 1
h.list1 = uicontrol(f,'Style','popupmenu');
h.list1.Position = [20 75 60 20];
h.list1.String = h.ListOptions;
% Create menu 2
h.list2 = uicontrol(f,'Style','popupmenu');
h.list2.Position = [100 75 60 20];
h.list2.String = h.ListOptions(2:end); % Skip first option since it's selected by default
% Assign callback *after* handles are all created
h.list1.Callback = {@selectionCallbackFcn,h};
% include the handles ^
% (if not using GUIDE or appdesigner)
%
% In GUIDE, "handles" is already included,
% you just need to add the master list to
% it using guidata().
% https://www.mathworks.com/help/matlab/ref/guidata.html
%
% In AppDesigner, "app" is already included,
% you just need to use custom properties.
% https://www.mathworks.com/help/matlab/creating_guis/share-data-across-callbacks-in-app-designer.html
function selectionCallbackFcn(hObj,~,handles)
% Popup menu callback function
% Responds to selection from popup menu #1
% Determine which string is selected in each list
selection1 = hObj.String{hObj.Value};
selection2 = handles.list2.String{handles.list2.Value};
% Remove selected string from list 2 & assign to list 2
handles.list2.String = handles.ListOptions(~strcmp(handles.ListOptions,selection1));
% Maintain the selected item in list2 unless it was just removed
handles.list2.Value = max([1;find(strcmp(handles.list2.String,selection2))]);
end
  4 Comments
Stefany Romero
Stefany Romero on 30 Sep 2019
Mr Adanz, sorry but deleted the question for error.
I'm working on guide with MATLAB R2018A, please.
I've pasted my code, i hope you can help me.
value_in_str = handles.edit1.String;
switch handles.popupmenu1.Value
case 1
msgbox('No ha seleccionado ninguna base a convertir');
case 2
value_in_dec = bin2dec(value_in_str);
case 3
value_in_dec = hex2dec(value_in_str);
case 4
value_in_dec = sscanf(value_in_str,'%lo')
case 5
value_in_dec = sscanf(value_in_str,'%ld');
end
switch handles.popupmenu2.Value
case 1
msgbox('No ha seleccionado ninguna base a convertir');
case 2
value_out_str = dec2bin(value_in_dec);
case 3
value_out_str = dec2hex(value_in_dec);
case 4
value_out_str = sprintf('%lo',value_in_dec)
case 5
value_out_str = sprintf('%ld',value_in_dec);
end
handles.edit2.String = value_out_str;
Adam Danz
Adam Danz on 1 Oct 2019
Thanks for reverting the question.
In the code you shared, I don't see where you've implemented the selectionCallbackFcn function. Have you added a selection callback to your popupmenu?

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!