Error Messages

Hi Folks,
I have generated my code depending on what the user inputs. So, my questions in the command window is basically numbers corresponding to particular Methods that I have coded. However, now, if the user inputs a character or number outside those input number range the question asks, by mistake, the code continues and gives an error later down. I would like it to stop there temperarily and requestion the user with the same question until he/she gets it correct.
disp('Please Enter the Method:')
disp('1 – Aligrot Method')
disp('2 – Pishinger Method')
disp('3 – Watson Method')
UserInput = input('Enter the Number corresponding to the selective Method and Press ENTER: ');
if UserInput == 1
%calls a set of functions
elseif UserInput == 2
%calls a set of functions
elseif UserInput == 3
%calls a set of functions
elseif 3 < UserInput < 1 || ischar(UserInput )
error('PLEASE READ CAREFULLY!!');
end
Thanks

2 comentarios

Daniel Shub
Daniel Shub el 26 de Abr. de 2012
What is the problem you are having? What happens and what do you expect to happen?
Ferd
Ferd el 26 de Abr. de 2012
Suppose the User enters a number 5(say). The number 5 doesn't correspond to those numbers I've specified. I would like it to requestion the User to input the correct number, hence the correct Method. Similarly, if the user inputs a letter it should do the same, ask the question again until the right number is selected.

Iniciar sesión para comentar.

 Respuesta aceptada

Daniel Shub
Daniel Shub el 26 de Abr. de 2012

0 votos

Your if statement can be made to work, but I think a switch statement is a little cleaner.
doc switch
There are lots of ways of handling the exiting from the loop. You might want to use
doc break
I went with a TF flag that is either true or false.
TF = true;
while TF
TF = false;
disp('Please Enter the Method:')
disp('1 – Aligrot Method')
disp('2 – Pishinger Method')
disp('3 – Watson Method')
UserInput = input('Enter the Number corresponding to the selective Method and Press ENTER: ');
switch UserInput
case 1 %calls a set of functions
case 2 %calls a set of functions
case 3 %calls a set of functions
otherwise
warning('PLEASE READ CAREFULLY!!');
TF = true;
end

1 comentario

Ferd
Ferd el 26 de Abr. de 2012
Excellent! thanks!
Yeah, its cleaner. It just selects for only those specified numbers other numbers or characters are eliminated without specifying them.
Will have to include the switch function in the other User inputs I've coded...
Thanks again!

Iniciar sesión para comentar.

Más respuestas (1)

Daniel Shub
Daniel Shub el 26 de Abr. de 2012

0 votos

Why not get fancy:
UserInput = questdlg('Select a method.', 'Method List', ...
'Aligrot', 'Pishinger', 'Watson', 'Aligrot')
switch lower(UserInput)
case 'aligrot' %calls a set of functions
case 'pishinger' %calls a set of functions
case 'Watson' %calls a set of functions
end

7 comentarios

Ferd
Ferd el 26 de Abr. de 2012
haha... Yea, will do...
Ferd
Ferd el 26 de Abr. de 2012
Whilst reading about the questdlg I also read inputdlg box and have added both in my code. Just a quick question, can we have an analog meter inputdialog box?
or I may be getting a bit too fancy...
Walter Roberson
Walter Roberson el 26 de Abr. de 2012
What would you like an "analog meter inputdialog box" to look like?
Image Analyst
Image Analyst el 26 de Abr. de 2012
For more than 3 items, you can have a popup dialog box with a vertical array of buttons by using the menu() function. It returns the number of the button you clicked on and then you can check that button number with an if or switch statement.
Ferd
Ferd el 26 de Abr. de 2012
Basically, the way I set my equation in the code is that it can taken in a single value between 0.1 to 1. As of now, I simply use the "input" function to get the value. Is it possible to have an analog meter scroll bar that has limits from 0.1 to 1? So, the user can simply horizontally shift the pointer to what value is preferred instead of writing it in the command window?
Or would I have to create a GUI for it?
Walter Roberson
Walter Roberson el 26 de Abr. de 2012
Everything in the graphics system is a GUI, including inputdialog. Some of the GUI are pre-written, that's all.
I do not recall any pre-written sliders at the moment, but sliders are not difficult to implement. You might want to check out (e.g.) "41 GUI examples" from the file exchange.
Ferd
Ferd el 27 de Abr. de 2012
Kewl thanks!

Iniciar sesión para comentar.

Categorías

Más información sobre App Building en Centro de ayuda y File Exchange.

Preguntada:

el 26 de Abr. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by