producing an error message based on response in prompt message box
8 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Voss
2024-3-16
编辑:Voss
2024-3-16
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!