File Exchange

image thumbnail

Generalised question dialog (questdlg)

version 1.1 (3.86 KB) by

A question dialog (questdlg) alternative

14 Downloads

Updated

View License

Create and open a push button dialog box, which is a generalized version of a question dialog box (called by questdlg). User can enter any number of input options (buttons)for one to choose from, as opposed to questdlg, where only 2 or 3 options are supported. User can also set the buttons ordering (columns, rows). Dialog will attempt to set parameters to optimally present button text.

Comments and Ratings (17)

Vojtech

Arnold Soltz

To make it truly bullet-proof you need to insert FIVE lines of code before the uiwait;

if islogical(iDefOption),
iDefOption = find(iDefOption,1,'first');
if isempty( iDefOption), iDefOption = 0; end;
end;
choise = iDefOption;
uiwait(menuFigH); % wait untill user makes his choise, or closes figure

The modified function will always return, always close the figure, and always output a scalar(double) integer. It is possible for the output scalar to equal zero, if (and only if) the caller has supplied an incorrect setting for the input variable "defOption". You can't test for everything.

Arnold Soltz

This works except for one case:
If the user closes the figure without making a selection then the output variable 'choise' will not be defined. 'Choice' should be set to the default just before the uiwait on line 194. Choice depends on iDefOption. A difficulty is that the variable 'iDefOption' in line 193 may be either a logical array or a numerical scalar.
The fix is to make sure that 'iDefOption' is a numerical scalar and then set choise to that value.
I inserted 4 lines of code just before the uiwait:
if islogical(iDefOption),
iDefOption = find(iDefOption,1,'first');
end;
choise = iDefOption;
uiwait(menuFigH); % wait untill user makes his choise, or closes figure

When the caller closes the figure without making a choice, the code now works the way the documentation says: it ALWAYS returns a scalar integer. It is possible for the function to return zero (if the default input variable is incorrect), and the caller might have to test for this.

Radha Kumari

how do I close it,
delete(menuFigH) or delete(figH) don't seem to work, even when my main function is running or when the main function using it is exited, it won't close by itself or using aforementioned commands

Radha Kumari

I like it, thank you :)

Doug Welling

Valentino

Works good for me just changed the setting that if the box is closed it buts choise to 0 like this

A= exist('choise');
if A==0;
%choise does not exist (closed)
choise =0;
end

anyways really helpful

Nikolay S.

Nikolay S. (view profile)

Hi John.
My program block Matlab execution using the "uiwait" command, until the user makes his choise. Along other it blocks opening figures browsers, such as "uigetfile".
The function can be changed, but I believe a much better option will be to call "uigetfile" after the user has clicked the right button- after the dialog window is closed. This will solve your issue.
Try this code (based on your question):

inputOptions={'Opt1', 'Opt2', 'Opt3','Exit'};
defSelection=inputOptions{end};
iSel=bttnChoiseDialog(inputOptions,...
'My Helpful Program',...
defSelection,...
'Please select a program option, or exit to end.');
ActRqst = inputOptions{iSel};
switch ActRqst
case(inputOptions{1})
% Do the right thing
fprintf('Dummy action. You choose: %s\n', inputOptions{1});
case(inputOptions{2})
% Do the right thing
fprintf('Dummy action. You choose: %s\n', inputOptions{2});
case(inputOptions{3})
% Do the right thing
fprintf('Dummy action. You choose: %s\n', inputOptions{3});
case(inputOptions{4})
% Do the right thing
fprintf('Dummy action. You choose: %s\n', inputOptions{4});
end

Best regards.

John Baldauf

Having a problem when using this function. I use it to present a set of options, then switch the result. One of the cases executes a function I wrote. That function utilizes the standard matlab function 'uigetfile'. When I run the program and choose that case, the uigetfile window never opens. It just acts as if someone instantly closed the window, which provides empty outputs, throwing an error. To confirm, I commented out bttnChoiseDialog and forced to option 3, as shown below. My code worked again. Any idea what is going on? What can be done to fix? I am running 7.11.0 (R2010b). Thanks!

inputOptions={'Opt1', 'Opt2', 'Opt3','Exit'};
% defSelection=inputOptions{end};
% iSel=bttnChoiseDialog(inputOptions,...
% 'My Helpful Program',...
% defSelection,...
% 'Please select a program option, or exit to end.');
ActRqst = inputOptions{3};

Dear Nikolay S. and Dan K.,

A very happy new year to you two also. After Dan's modification I am having no problem running this .m file. It has been hugely helpful, so my thanks to you both.

Louis

Nikolay S.

Nikolay S. (view profile)

Good evening and Happy New Year Dan K and Louis Vallance. Thank you for your comments. it seems you both are right, while I was wrong. indeed this failed to work. Dan K proposal fixes the problem. My compliments

Hi again.

I tested the example provided in your code and iSel is 3 (the default selection) regardless of which button is pressed. Is there a fix for this, cause otherwise the program doesn't work.

Cheers

Hi Nikolay,

With the old questdlg I could just say variable=questdlg(inputs) and the button I pressed would be assigned to variable. With this function it doesn't seem to be working.

Dan K

Dan K (view profile)

Hi Nikolay,

Thanks for responding. I am running R2012a(7.14.0.739). What example are you using?

For testing, I called both the FEX code, and the modified code with the following syntax:

>> selectedIndex=bttnChoiseDialog({'str1', 'str2', 'Cancel'}, 'Selection', 'Cancel','Which str?')

With the original code, selectedIndex is set to 3 for pushes on either str1 or str2 (the default button, "cancel" is at index #3 and has it's "value" property set to true, which is then found in the function line: "choise=find( cell2mat( get(buttnHndl, 'Value') ) ); "). The modified code returns 1 for str1 and 2 for str2 (using the handle of the object that executed the callback, it does not have to find a true or false value).

Nikolay S.

Nikolay S. (view profile)

Good evening Dan. Thank you for your comment. As far as I can see you've changed the callback function, replacing 'uiresume(gcbf)' with your my_bttnCallBack service function.
I've tried your code with my exmaple, but it resulted in an error:
"Error in bttnChoiseDialog>my_bttnCallBack (line 201)
choise=find(strcmp(dlgOptions, get(hObject,'String')));

Error using waitfor
Error while evaluating uicontrol Callback"

My original code seems to work fine for the given example. i'm using Matlab 2011b. What version do you run?
Best regards.

Dan K

Dan K (view profile)

Dan K

Dan K (view profile)

The end of this function was not working as I was using it, so I changed this:

for iBttn=1:nButtons
[iRow, iCol]=ind2sub([bttnRows, bttnCols],iBttn);
currBttnHeigth=figH-titleSps-iRow*interBttnStpH;
currBttnLeft=(iCol-1)*interBttnStpW+spaceW+(interBttnStpW-bttnW-spaceW)/2;

buttnHndl(iBttn) = uicontrol( buttonGroup, 'Style', 'pushbutton',...
'FontUnits', 'normalized', 'Units', 'characters', 'FontSize', bttnFontSize,...
'String', dlgOptions{iBttn}, 'Callback', 'uiresume(gcbf)',...
'Position', [currBttnLeft , currBttnHeigth, bttnW, bttnH] );
end
set(buttnHndl(iDefOption), 'Value', 1); % set default option
set(cat(1, buttnHndl, titleH, buttonGroup),'Units', 'normalized')

uiwait(menuFigH); % wait untill user makes his choise, or closes figure

choise=find( cell2mat( get(buttnHndl, 'Value') ) );
delete(menuFigH);

to

for iBttn=1:nButtons
[iRow, iCol]=ind2sub([bttnRows, bttnCols],iBttn);
currBttnHeigth=figH-titleSps-iRow*interBttnStpH;
currBttnLeft=(iCol-1)*interBttnStpW+spaceW+(interBttnStpW-bttnW-spaceW)/2;
buttnHndl(iBttn) = uicontrol( buttonGroup, 'Style', 'pushbutton',...
'FontUnits', 'normalized', 'Units', 'characters', 'FontSize', bttnFontSize,...
'String', dlgOptions{iBttn}, 'Callback', @my_bttnCallBack,...
'Position', [currBttnLeft , currBttnHeigth, bttnW, bttnH]);
end
set(buttnHndl(iDefOption), 'Value', 1); % set default option
set(cat(1, buttnHndl, titleH, buttonGroup),'Units', 'normalized')

uiwait(menuFigH); % wait untill user makes his choise, or closes figure

delete(menuFigH);

function my_bttnCallBack(hObject, ~)
choise=find(strcmp(dlgOptions,get(hObject,'String')));
uiresume(gcbf)
end

end

Updates

1.1

An issue reported by Dan K and Louis Vallance was fixed, using Dan K proposal.

MATLAB Release
MATLAB 7.12 (R2011a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video