How can i display once a warning?
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Stephen23
2017-2-10
编辑:Stephen23
2017-2-10
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 个评论
更多回答(2 个)
Image Analyst
2017-2-10
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 个评论
Image Analyst
2017-2-11
编辑:Image Analyst
2017-2-11
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!