If prompt answers are wrong format or empty, stop code
2 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
回答 (2 件)
Image Analyst
2017 年 7 月 17 日
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 件のコメント
Image Analyst
2017 年 7 月 18 日
Just use [] to group them:
startAndEnd = [usersValue1, usersValue2];
Derick Yang
2017 年 7 月 17 日
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))
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!