Info

# I need to generate a warning statement and I am not exactly sure how.

1 view (last 30 days)
Anthony Campuzano on 1 Jul 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
I am having a hard time with the warning statement for this problem.
Says "If the goal is not met in 10 years, a warning should be generated" heres my code
function [yrs, F] = InvestGoal(P,G,i)
%InvestGoal determines the future worth of an investment
%
%INPUT P: the principle amount invested
% G: the goal amount
% i: expected yearly compunded interest
%OUTPUT yrs: number of years needed to reach the goal
% F: the amount the investment is worth at the final year
if numel(P) > 1 || numel(G) > 1 || numel(i) > 1
error('only a single number for each input')
elseif P <= 0 || G <= 0 || i <= 0
error('All inputs must be positive numbers')
elseif G <= P
error('Principle must be less than the Goal amount')
end
disp('Year Future Worth (\$)');
A=P;
x=1;
while A < G
A=P*(1+i)^x;
fprintf(' %d %.2f\n',x,A);
x = x + 1;
end
yrs = x - 1;
F = A;
end
dpb on 1 Jul 2015
See
doc warning % maybe?

James Tursa on 1 Jul 2015
E.g., put this at the end of your function:
if( yrs > 10 )
warning('Goal not reached in 10 years');
end

Image Analyst on 2 Jul 2015
Try
if yrs > 10
message = sprintf('Warning: It has been %d years.\nThe goal was not attained in less than 10 years', yrs);
uiwait(warndlg(message));
end
warndlg() will generate a more user friendly and noticeable popup warning message, and, importantly, wait for the user to click OK before proceeding on with the rest of the code. If you omit uiwait(), then warndlg() will just popup a message and continue on with whatever code follows.