How can i display once a warning?
5 views (last 30 days)
Show older comments
Hi. I have a code as below:
x=input('Please enter value of x: ');
y=input('Please enter value of y: ');
z=input('Please enter value of z: ');
V={'x','y','z'};
Ktra=[x y z];
for i=1:numel(Ktra)
if Ktra(i)<0
disp('Value enter into must positive');
fprintf('Value of %s is negative \n',V{i});
end
end
So, If i enter x = -9 and y = -6, program will give:
Value enter into must positive
Value of x is negative
Value enter into must positive
Value of y is negative
And you can see, we have two warnings: "Value enter into must positive". It is unfavorable.
So, how can i do to display that warning once?
I mean that no matter how much I enter, it is just given a warning message. As below:
Value enter into must positive
Value of x is negative
Value of y is negative
Thank you so much!
0 Comments
Accepted Answer
Stephen23
on 10 Feb 2017
Edited: Stephen23
on 10 Feb 2017
N = {'x','y','z'};
V = nan(size(N));
for k = 1:numel(V)
str = sprintf('Please enter value of %s: ',N{k});
V(k) = str2double(input(str,'s'));
end
if any(V<0)
fprintf('Value enter into must positive\n')
end
for k = reshape(find(V<0),1,[])
fprintf('Value of %s is negative\n',N{k});
end
and tested:
Please enter value of x: -6
Please enter value of y: -9
Please enter value of z: 4
Value enter into must positive
Value of x is negative
Value of y is negative
Note that, as you were told in your last question, it is faster and safer to call input with its second 's' option, which prevents input from evaluating anything that the user might supply. Using this option is highly recommended.
4 Comments
More Answers (2)
Image Analyst
on 10 Feb 2017
I'd recommend a more in-your-face approach using errordlg():
errorMessage = sprintf('You must enter a positive value.\nYou entered %f, which is negative.\nPlease try again.', Ktra(i));
uiwait(errordlg(errorMessage));
0 Comments
Image Analyst
on 11 Feb 2017
Edited: Image Analyst
on 11 Feb 2017
I prefer a more user friendly way to ask the user for numbers, like this:
% Ask user for three floating point numbers.
defaultValue = {'10', '20', '30'};
titleBar = 'Enter a value';
userPrompt = {'Enter x: ', 'Enter y: ', 'Enter z: '};
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})
usersValue3 = str2double(caUserInput{3})
% 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 usersValue1 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
% Check usersValue3 for validity.
if isnan(usersValue3)
% 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 usersValue3.
usersValue3 = str2double(defaultValue{3});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue3);
uiwait(warndlg(message));
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/177119/image.png)
0 Comments
See Also
Categories
Find more on Ordinary Differential Equations 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!