MATLAB Answers

How to use questdlg to ask the user for an action?

8 views (last 30 days)
Cantor Set
Cantor Set on 20 Oct 2019
Edited: John D'Errico on 20 Oct 2019
I want to ask the user to make a choice on which function to excute
the algorithm is as follows:
  1. Ask the user "what type of selection?"
  2. possible answers a) random b)roulette-wheel 3)tournment
  3. if the answer is random selection, excute the random.m file
  4. if the answer is roulette-wheel , excute the roulette-wheel.m file
  5. if the answer is tournment, excute the tournment.m file
Not sure how to do it
i tried this but caught me in an infinite loop.
%x,y,z are defined in the actual code
answer = questdlg('choose a selection method?', ...
'selection methods', ...
'random','roulette-wheel','tournment');
% Handle response
switch answer
case 'random'
selch=random(x,y,z)
case 'roulette-wheel'
selch=roulette-wheel(x,y,z)
case 'tournment'
selch=tournment(x,y,z);
end

  0 Comments

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 20 Oct 2019
Edited: John D'Errico on 20 Oct 2019
Your first problem is you are not using it correctly.
The last argument should be one of the choices, to give it a default button selection.
answer = questdlg('choose a selection method?', ...
'selection methods', ...
'random','roulette_wheel','tournment','random');
(You spelled tournament incorrectly, not that it matters.)
But the above will give you a dialog box with three options, one of which (the first) is highlighted.
How it caught you in an infinite loop is difficult to know, since we lack your other code. Perhaps something in your code calls itself recursively, although if it did, then you would get a stack limit exceeded error, not a true infinite loop, because it will crash long before things go near infinite.
Another error in the code is you cannot name a function roulette-wheel. You CAN name it roulette_wheel, which is what I put in the call to questdlg. A minus sign cannot be part of a function name, but an underscore can be.

  0 Comments

Sign in to comment.

Sign in to answer this question.