필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

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

조회 수: 1 (최근 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

답변 (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