producing an error message based on response in prompt message box

I want to include a prompt in my matlab code and have the user specify a value between 0 and 3. This value then updates a different variable deeper in the code. If the user types a number less than or greater than these values I want an error box to pop up and say they should choose a different value. The first issue im running into is the "or" logic in the for loop, if the value is less than 0 or greater than 3 is what I want. The second issue is when a I type a value between 0 and 3 the error box still pops up but the code continues, I dont want the error box to pop up and im not sure how to fix it. Ive been working on this for several hours and feel silly for not figuring it out. I have a second error message for the second question that says if its not one of the 10 accepted answers then pop up an error message, thats not working either but ill figure it out later. Thanks!
prompt = {'Enter Figure Type:','Enter Output Type:'};
dlgtitle = 'Input';
fieldsize = [1 45; 1 45];
answer = inputdlg(prompt,dlgtitle,fieldsize);
if answer{1} < 0 || answer{1} > 3
f2 = msgbox("Invalid input, choose a number 0 and 1", ...
"Error", "error");
end
% if answer{2} ~= 00 && 01 && answer{2} ~= 10 && answer{2} ~= 11 &&
% answer{2} ~= 20 && answer{2} ~= 21 && answer{2} ~= 30 && answer{2} ~= 31
% f2 = msgbox("Invalid input, choose an output option x0 or x1", ...
% "Error", "error");
% end

Respuestas (1)

Voss
Voss el 16 de Mzo. de 2024
Editada: Voss el 16 de Mzo. de 2024
prompt = {'Enter Figure Type:','Enter Output Type:'};
dlgtitle = 'Input';
fieldsize = [1 45; 1 45];
answer = inputdlg(prompt,dlgtitle,fieldsize);
If the user clicked OK, then answer is a cell array of character vectors, e.g., {'0';'1'} if the user entered 0 and 1. You need to convert those character vectors to numeric before validating their values. That's why the comparison is not working properly - you are comparing a character vector, e.g., '0' to a number, e.g., 0.
Also you need to check that those character vectors are not empty, i.e., that the user entered something for each prompt, and you need to handle the case that the user closed the input dialog figure without clicking OK (i.e., clicked Cancel or just closed the dialog), in which case answer is an empty cell array.
if isempty(answer)
% user cancelled or closed
% you decide what it makes sense to do in this case
% maybe return?
else
% user clicked OK
answer = str2double(answer); % convert each character vector to a scalar numeric
% if the user didn't enter anything or entered something that can't be made
% into a scalar number, e.g., 'baba' or '1 2', then that element of answer
% is NaN after doing str2double, so check for NaN here as well as
% checking the values:
if isnan(answer(1)) || answer(1) < 0 || answer(1) > 3
f2 = msgbox("Invalid input, choose a number between 0 and 3", ...
"Error", "error"); % maybe use errordlg instead of msgbox? doesn't really matter
% maybe return here?
end
end

5 comentarios

Thanks again btw for answering another question of mine, its very helpful. Im have a tight deadline to process a massive amount of data (a year seems like a long time but its alot of data).
Ive been doing some more research and this is where Im at, below is the most up to date code I have for this section. I put it all in a while loop but Im stuck in that loop now.
InputAnswer = true;
while InputAnswer
InputAnswer = false;
prompt = {'Enter Figure Type:','Enter Output Type:'};
dlgtitle = 'Input';
fieldsize = [1 45; 1 45];
answer = inputdlg(prompt,dlgtitle,fieldsize);
if answer{1} < 0
f2 = sprintf("Invalid input, choose a number between 0 and 3");
l1 = msgbox(f2, "Error", "error");
InputAnswer = true
elseif answer{1} > 3
f3 = sprintf("Invalid input, choose a number between 0 and 3");
l2 = msgbox(f3, "Error", "error");
InputAnswer = true
else
InputAnswer = false;
end
end
Also, when I update the variable later to determine what method of figure to output im using this:
Figure_Output = str2double(answer{1});
Output_Option = str2double(answer{2});
If I comment out the > 3 line and input a value greater than 0 it works fine. When I negative value the code stops because none of the options I have set up are equal to -1, however I dont get the error message I want and I dont return to the while loop. If I leave the >3 option I get stuck in an endless loop. Thanks again.
Voss
Voss el 16 de Mzo. de 2024
Editada: Voss el 16 de Mzo. de 2024
You need to do the str2double before error-checking the entered values because they are not numeric before that (they are character vectors).
Also, don't set InputAnswer = false; until those answers have been validated. This way the while loop can continue running until you get all good answers. Or you can do away with InputAnswer and do while true (so the loop runs until hitting a break statement) and break when you know the answers are valid.
% these can be defined before the while loop starts, because they don't
% ever change:
prompt = {'Enter Figure Type:','Enter Output Type:'};
dlgtitle = 'Input';
fieldsize = [1 45; 1 45];
while true
answer = inputdlg(prompt,dlgtitle,fieldsize);
if isempty(answer)
% user closed the dialog
continue % ask again
end
% convert to numeric:
answer = str2double(answer);
if isnan(answer(1)) || answer(1) < 0 || answer(1) > 3 % check if answer 1 is valid
uiwait(msgbox("Answer 1 is invalid, choose a number between 0 and 3", "Error", "error"));
elseif isnan(answer(2)) || answer(2) < 0 || answer(2) > 3 % check if answer 2 is valid (I don't know the conditions for this)
uiwait(msgbox("Answer 2 is invalid, choose a number between 0 and 3", "Error", "error"));
else % both valid input
break % exit the while loop
end
end
Figure_Output = answer(1);
Output_Option = answer(2);
I've used uiwait to pause execution of the code until the user closes the error message box, so you don't pop up the next inputdlg until after that (i.e., to avoid getting multiple dialog boxes up at the same time).
I got it to work with this, however it doesnt return to the input section if an invalid input is given.
prompt = {'Enter Figure Type:','Enter Output Type:'};
dlgtitle = 'Input';
fieldsize = [1 45; 1 45];
answer = inputdlg(prompt,dlgtitle,fieldsize);
answer = str2double(answer);
if answer(1) < 0
f2 = sprintf("Invalid input, choose a number between 0 and 3");
l1 = msgbox(f2, "Error", "error");
end
if answer(1) > 3
f2 = sprintf("Invalid input, choose a number between 0 and 3");
l1 = msgbox(f2, "Error", "error");
end
See my previous comment for running that inside a while loop, which causes the user to be prompted until all answers given are valid.
Is it working now?

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2023b

Etiquetas

Preguntada:

el 16 de Mzo. de 2024

Comentada:

el 20 de Mzo. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by