Info

この質問は閉じられています。 編集または回答するには再度開いてください。

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

3 ビュー (過去 30 日間)
Anthony Campuzano
Anthony Campuzano 2015 年 7 月 1 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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
  1 件のコメント
dpb
dpb 2015 年 7 月 1 日
See
doc warning % maybe?

回答 (2 件)

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

Image Analyst
Image Analyst 2015 年 7 月 2 日
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.

この質問は閉じられています。

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by