3.33333

3.3 | 3 ratings Rate this file 25 Downloads (last 30 days) File Size: 3.86 KB File ID: #37261
image thumbnail

Generalised question dialog (questdlg)

by

 

21 Jun 2012 (Updated )

A question dialog (questdlg) alternative

| Watch this File

File Information
Description

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.

Required Products MATLAB
MATLAB release MATLAB 7.12 (R2011a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (10)
04 Aug 2014 Nikolay S.

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.

05 Feb 2014 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};

10 Jan 2013 Louis Vallance

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

02 Jan 2013 Nikolay S.

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

19 Dec 2012 Louis Vallance

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

15 Dec 2012 Louis Vallance

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.

14 Sep 2012 Dan K

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).

14 Sep 2012 Nikolay S.

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.

17 Aug 2012 Dan K  
17 Aug 2012 Dan K

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
02 Jan 2013

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

Contact us