# All Purpose Mortgage Calculator including mortgage schedule2

Posted by Sean de Wolski,

Sean's pick this week is All Purpose Mortgage Calculator including mortgage schedule by S B.

I am currently in the fun process of buying a house. My wife and I were going over the mortgage calculations the other day and figuring out our approximate monthly payments and the amount of money we want to put down. We know that the financial company we are using probably knows how to do these calculations. But we wanted to check them! I also noticed that they were using Excel so that made me curious to see how easy this would be in my choice software for everything, MATLAB.

It's generally the case that if functionality for MATLAB seems obvious, it's probably built in to the product or one of the toolboxes. If this is not the case, then someone else has likely already written it and deposited it on the File Exchange. I vaguely remembered this App from a few months ago when my friend Doug and I were talking about the great low interests rates available right now.

NOTE All numbers below have been obfuscated to hide the actual financial details.

### Apps

In MATLAB R2012b, we introduced MATLAB Apps and the Apps Toolbar:

Using MATLAB Apps you can package your GUIs and their dependencies into single files that can be distributed and installed easily. This will help your users avoid the standard path issues. Apps also enable you to have easy version control and descriptions for your GUIs.

Side Note: I know that during the R2012b release many of you wanted to be able to change the App icon. In R2013a, the ability to select an icon from one of a dozen is available.

### This App

So let's take a look at this App:

We'll first try 20% down:

How does that compare to 5% down (not including Private Mortgage Insurance (PMI)):

So the total interest saved by putting down 20% instead of 5% is about \$28K, not including PMI.

What if we could shorten it to 15 years?

This just about halves the amount of interest!

Unfortunately, the plot does not work for any number of years less than 30 (enhancement!)

For every scenario, the numbers lined up with those that the financial firm gave us verifying both their calculations and this App.

### The Code

Though we have a big suite of Financial Products, none of them were used here. The engine behind this appears to be about 20 lines of MATLAB code and everything else is just for the GUI.

One thing I noticed is that S B did a lot of work for selecting the colors for the plot lines. Here's what he did:

color_map = ['rbgrbmbrgr'];
z1 = 0;
z2 = 0;

while (z1 == 0) | (z1 > 9) | (z2 == 0) | (z2 > 9)

z1 = fix(rand(1)*10);
z2 = fix(rand(1)*10);

% What are the chances of get the same random number twice!?
if (z1 == z2)
z1 = fix(rand(1)*10);
z2 = fix(rand(1)*10);
end
end
z1 = color_map(z1);
z2 = color_map(z2);

I would recommend just using randperm() to guarantee two unique integers between one and nine.

color_map = 'rgbcmyk';
idx       = randperm(numel(color_map),2);
color1    = color_map(idx(1))
color2    = color_map(idx(2))
color1 =

k

color2 =

b

### Some Suggested Future Enhancements

There are a few enhancements that would make this App even better!

1. The ability for it to plot for a term different than 30 years.
2. The ability to account for overpayments (e.g. by just changing the monthly amount).
3. The ability to account for PMI.

So the answer to my original question: How easy would it be to do this in MATLAB? Really easy!

Get the MATLAB code

Published with MATLAB® R2013a

Dick Walvis replied on : 1 of 2

Thank you.

One of the commenters asked for regular currency format. uitable does not make that easy. When converting to text, all entries are left aligned which looks ugly. Does uitable have the option to right align text? (I could not find it.)

An awkward solution is to create text numbers with leading 0’s and then replacing leading zeros with spaces. Am I overlooking something?

Sean de Wolski replied on : 2 of 2

Hi Dick,

Unfortunately there is no “ColumnJustify” property. I will add this as an enhancement request.

Rather than adding and stripping off zeros, you should be able to do this explicitly and automatically with SPRINTF using the ‘ ‘ flag. Here is an example:

figure;
n = 20;
hT = uitable('Units','normalized',...
'Position',[0.2 0.2 0.6 0.6],...
'ColumnFormat',{'char'},...
'FontName','monospaced');
amount = rand(n,1)*1000;
data = cell(n,1);
for ii = 1:n
data{ii} = sprintf('% 10.2f',amount(ii));
end
set(hT,'Data',data)
%Equivalent one-liner
%{
C = cellstr(reshape(sprintf('% 10.2f',amount).',[],n)');
set(hT,'Data',C)
%}

Regards,
%Sean

ps. typically you can get a wider range of replies by posting a question like this to MATLAB Answers: