본문 바로가기

MATLAB/ㄴ 앱 디자이너

숫자형 입력값을 기반으로 데이터를 계산하고 플로팅하는 앱 만들기

 

https://kr.mathworks.com/help/matlab/creating_guis/mortgage-app-or-gui-in-app-designer.html#d126e5803

 

숫자형 입력값을 기반으로 데이터를 계산하고 플로팅하는 앱 - MATLAB & Simulink - MathWorks 한국

이 예제의 수정된 버전이 있습니다. 사용자가 편집한 내용을 반영하여 이 예제를 여시겠습니까?

kr.mathworks.com

 

 

 

핵심코드

 

'Monthly Payment' 버튼에 Push 시 이벤트가 발생하는 콜백함수를 삽입함.

 


        % Button pushed function: MonthlyPaymentButton // MonthlyPayment 버튼을 클릭 시 발생하는 이벤트 함수
        function MonthlyPaymentButtonPushed(app, event)
        
            % Calculate the monthly payment // 월 납입액을 계산
            amount = app.LoanAmountEditField.Value;
            rate = app.InterestRateEditField.Value/12/100;
            nper = 12*app.LoanPeriodYearsEditField.Value;
            payment = (amount*rate)/(1-(1+rate)^-nper);
            app.MonthlyPaymentEditField.Value = payment;
            
            % pre allocating and initializing variables // 변수를 할당한 후 초기화
            interest = zeros(1,nper);
            principal = zeros(1,nper);
            balance = zeros(1,nper);
            balance(1) = amount;
            
            % Calculate the principal and interest over time // 원금과 이자의 계산
            for i = 1:nper
                interest(i)  = balance(i)*rate;
                principal(i) = payment - interest(i);
                balance(i+1) = balance(i) - principal(i);
            end
            
            % Plot the principal and interest // 원금과 이자를 시각화
            plot(app.PrincipalInterestUIAxes, (1:nper)', principal, (1:nper)', interest); % 그래프
            legend(app.PrincipalInterestUIAxes,{'Principal','Interest'},"Location","Best") % 범례
            xlim(app.PrincipalInterestUIAxes,[0 nper]);  % X축
            ylim(app.PrincipalInterestUIAxes,"auto"); % Y축

        end