MATLAB: How can i display once a warning

warning

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!

Best Answer

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.