If prompt answers are wrong format or empty, stop code
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Image Analyst
el 17 de Jul. de 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 comentarios
Image Analyst
el 18 de Jul. de 2017
Just use [] to group them:
startAndEnd = [usersValue1, usersValue2];
Derick Yang
el 17 de Jul. de 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))
Ver también
Categorías
Más información sobre Startup and Shutdown en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!