If prompt answers are wrong format or empty, stop code

5 views (last 30 days)
Hi all. I have a prompt that asks five questions. And if for any reason the user enters an empty answer or in the incorrect format, the code should stop and return an error messagebox popup. I've tried a few things, but they only work if all the answers in the prompt are empty, but I need it so that if any is empty or incorrect format, the error pops up and stops the code. Any thoughts? Below is a part of the code.
prompt_full= {'Time span [start,end] (hrs):', 'Initial Pressure (psi):', 'Initial Concentration (decimal):', 'O2 Leakage Rate (lbm/hr):','# of Nodes:'};
dlog_title= 'User Input';
num_lines = 1;
default_answer= {'[0,12]','13.9','0.241','.000211','20'};
answer= inputdlg(prompt_full, dlog_title,num_lines,default_answer);
if cellfun(@isempty,answer)
msgbox('Error')
return
end
% if isempty(answer),return,end; %Cancel if empty
Value = str2double(answer);
if isnan(Value) %They entered a wrong input or clicked Cancel
msgbox('Inadequate Input. Please Try Again.');
return
end

Answers (2)

Image Analyst
Image Analyst on 17 Jul 2017
Try this snippet. Adapt as needed.
% Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter a value';
userPrompt = {'Enter floating point number 1 : ', 'Enter floating point number 2: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = str2double(caUserInput{1})
usersValue2 = str2double(caUserInput{2})
% Check usersValue1 for validity.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
% Do the same for usersValue2
% Check usersValue2 for validity.
if isnan(usersValue2)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue2.
usersValue2 = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue2);
uiwait(warndlg(message));
end
  2 Comments
Areeb Hussain
Areeb Hussain on 18 Jul 2017
Thank you for the help. The first answer in my prompt needs to be in the format:
[start,end]
including the brackets. Is there a way to check that the user put in that right format. It's not just a number. for example, it could be
[0,100]
Image Analyst
Image Analyst on 18 Jul 2017
Just use [] to group them:
startAndEnd = [usersValue1, usersValue2];

Sign in to comment.


Derick Yang
Derick Yang on 17 Jul 2017
The issue is the line:
if cellfun(@isempty, answer)
The output of cellfun here is a logical 5x1 array. In MATLAB, the if block will only evaluate when ALL elements of this logical array are true (which is why your code works if ALL the answers in your prompt are empty). You can edit this line as follows:
if any(cellfun(@isempty, answer))
Docs for any
  1 Comment
Areeb Hussain
Areeb Hussain on 18 Jul 2017
Thanks Derick! This was pretty easy. Do you have any thoughts about the comment I posted under Image Analyst's answer?

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!