double action of GUIDE adapted drop down menu callback

19 views (last 30 days)
In my code, I still have some GUIDE-style callbacks of dropdown menus as follows (this is just example code):
% Value changed function: mydropdown_menu
function mydropdown_menu_Callback(app, event)
% Create GUIDE-style callback args - Added by Migration Tool
[hObject, ~, handles] = convertToGUIDECallbackArguments(app, event);
value = get(hObject,'Value');
if value==7
warndlg('warning message');
end
guidata(hObject, handles);
end
The issue is: when I make choice of value 7, then 'warning message' is displaed as intended, however, when I make next choice of any other value, the 'warning message' is displayed again. I do not have such a problem in Appdesigner-style value changed callbacks. How can I solve this problem?
  3 Comments
G A
G A on 13 Jun 2023
Edited: G A on 13 Jun 2023
Yes, Rik, you are right. Function is being called twice.
When I have changed callback for this (without using GUIDE-style):
function mydropdown_menu_Callback(app, event)
app.mydropdown_menu.ItemsData = 1:length(app.mydropdown_menu.Items);
SelectedItem = app.mydropdown_menu.Value;
if SelectedItem==7
uiwait(warndlg('message'));
end
end
After choosing value 7, the massage is subsequently displayed twice. Waiting for 'OK' but without waiting for change of menu item. Before, in GUIDE-style, it was waiting for a click on menu item. (With or without 'uiwait')
I have not tried puting breakpoints because I do not know where to put them at the moment.
What does mean "inserting keyboard"?
G A
G A on 13 Jun 2023
function mydropdown_menu_Callback(app, event)
SelectedItem = app.mydropdown_menu.Value
end
this code also displays on Matlab command window the chosen value twice. To avoid accidental double-click of mouse, I made a choice using up-down arrows and 'Enter' key.

Sign in to comment.

Accepted Answer

G A
G A on 14 Jun 2023
Edited: G A on 14 Jun 2023
A have found the answer to my question.
In GUIDE created and subsequently migrated to Appdesigner dropdown menu, the ValueChangedFcn callback reacts to change of value as well as to click (Changing the name from mydropdown_menu_callback to mydropdown_menuValueChanged did not help). The following code prevented calling twice my callback:
% Value changed function: MatrixM_menu
function mydropdown_menu_callback(app, event)
if strcmp(event.EventName,'Clicked')
return
elseif strcmp(event.EventName,'ValueChanged')
val = app.mydropdown_menu.Value; % The word in the drop down that is showing.
choices = app.mydropdown_menu.Items; % List of all the possible choices.
[~, selectedIndex] = ismember(val, choices); % The index of the selected item.
if selectedIndex==7
uiwait(warndlg('message'));
end
end
end
  2 Comments
Image Analyst
Image Analyst on 14 Jun 2023
Yeah, klunky/kludgy. It looks like this is a side effect of having it do a conversion of the GUIDE code. That's why I said to just create a new drop down native to App Designer and you won't have that problem. You'd just have two separate callbacks, or maybe you need just one of them like the ValueChanged event. Anyway, since you used my code, can you at least click the Vote icon for my answer. Thanks in advance. 🙂
G A
G A on 14 Jun 2023
Edited: G A on 14 Jun 2023
Sure, I will vote. You definitely deserve it:) I even accepted at first your ansver because it was some solution, however, I wanted to understand, why I have shuch a behaviour. BDW, this woud work now with the original after-migration code as well. Have not tested yet.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 13 Jun 2023
What if you add a "value changed callback" by right clicking on the dropdown list? Then have this
function mydropdown_menu_Callback(app, event)
displayedString = app.mydropdown_menu.Value
selectedIndex = app.mydropdown_menu.Item
if selectedIndex == 7
uiwait(warndlg('warning message'));
end
end
  5 Comments
G A
G A on 14 Jun 2023
Edited: G A on 14 Jun 2023
I think, I found some answer to my question. That may be so called 'mouse double click glitch' (my laptop has Win 10, AMD Rysen 5).
The difference between GUIDE-style and Appdesigner-style dropdown menus is that former acts at any click on option choice whereas the latter calls callback only after choice was changed. Therefore, if I have that glitch on my laptop, then it affects GUIDE-style menu. Though, it happens also when I use up-down arrows and Enter key.
Anyway, @Image Analyst, @Rik - thanks to you both!

Sign in to comment.

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!