MATLAB Answers

1

Why does QUESTDLG return the wrong button string if I use the TAB key to switch between different buttons and then hit the Return key using MATLAB 7.3 (R2006b)?

I would like to use the TAB key to switch between different buttons of a QUESTDLG.
At the moment I am only able to use the mouse to choose between the different buttons but not the TAB key. The buttons do get highlighted in order but when you hit the return key, the default selection is always returned.
For example:
ButtonName = questdlg('What is your favorite color?', ...
'Color Question', ...
'Red', 'Green', 'Blue', 'Green');
switch ButtonName,
case 'Red',
disp('Your favorite color is Red');
case 'Blue',
disp('Your favorite color is Blue.')
case 'Green',
disp('Your favorite color is Green.');
end
After executing the above code, pressing the TAB key causes different buttons to be highlighted but Green is always returned when you hit the return key.

3 Answers

Answer by MathWorks Support Team on 3 Sep 2009
 Accepted Answer

The ability to use the TAB key to switch between different buttons on a QUESTDLG dialog and activating the button via return key is not available in MATLAB.
To work around this issue you can use the use the TAB key to switch between different buttons on a QUESTDLG dialog and hit the spacebar key to return the correct button string.

  1 Comment

Thierry Dalon comments "I would appreciate MathWorks fixing this issue - see my fix proposal in my answer."

Sign in to comment.


Answer by Thierry Dalon on 11 Nov 2013
Edited by Thierry Dalon on 11 Nov 2013

This issue can be fixed easily by adding/editing 2 lines in questdlg: in the KeyPressFcn callback:
function cb_KeyPressFcn(obj,evd) % TD
switch(evd.Key)
case {'return','space'} % TD
ButtonName=get(get(figureHandle,'CurrentObject'),'String');
setappdata(figureHandle,'ButtonName',ButtonName);
uiresume(gcbf);
case 'escape'
doDelete
end
and after the uiwait change the line:
ButtonName=get(get(QuestFig,'CurrentObject'),'String');
into:
ButtonName=getappdata(figureHandle,'ButtonName');
if isempty(ButtonName), % no KeyPress,
ButtonName=get(get(figureHandle,'CurrentObject'),'String');
end
(The code of questdlg could be a little bit cleaned up. TMO no need for these DefaultWasPressed checks.) @Mathworks: any plan to fix it? (This issue is still present in R2013b)

  0 Comments

Sign in to comment.


Answer by Fernando Pacheco on 1 Feb 2017
Edited by Fernando Pacheco on 1 Feb 2017

Hi guys,
You can use this function: https://gist.github.com/tdalon/612239aa8345912885559000c9a4a0f0. Same as QUESTDLG plus but solves the issue that if user press ENTER
or probably they have already fixed it for new versions!
cheers, FP

  0 Comments

Sign in to comment.