Writing Your First Program

In this section we will show you how to write a simple program in Matlab. To speed up understanding, we will write a program that replicates the plot generated in the “Visual Display” section.

  • If you didn’t complete the previous sections, you can download the data series HERE. Once you have downloaded the Matlab file, place it in your active Matlab “Current Folder”.

  • Writing a program for everything you do in Matlab is the most efficient way of operating within Matlab. A program allows you to replicate difficult processes quickly, save your work when it is not complete, fix mistakes before they become problems, help you remember how you solved a particular problem and much more.

  • Start writing a new program by clicking new script at the top left of the Matlab window
_images/newscript.png
  • It is always wise to place some notes at the top of your program describing the programs purpose. It is also a good idea to place comments around confusing parts of your code. There are often times when you will be working as a team and everyone writes code differently. Do yourself and your team members a favor and explain what you are doing.

  • As a house keeping practice, I always write three lines of code at the top of my program to avoid loitering variables and figures:
    • “clear all” clears all variables from the Workspace
    • “close all” closes all open figures
    • “clc” clears the history of commands entered in the Command Window

  • Another important habit is to place a semi-colon ”;” at the end of every command in your program. This will surpress printing of the command in the Command Window. However, occationally it will be useful to allow the computer to print a particular command.

  • Type the commands in the script editor in the order that you would like Matlab to execute them:
_images/first1.png
  • It is important to notice that if your variables are not saved in a file like “data.mat”, they will be cleared by the first line of code

  • Once you have written your code you will need to save it. Click on “Save” near the upper left of your Matlab window. Let’s name the program “plotSP”

  • Once the file is saved you are free to run the program by clicking “Run” or by typing “plotSP” and enter in the command window
_images/run.png
  • You should get a figure that looks like:
_images/plot2.png
  • Suppose that after you look at the graph, you decide that the real interesting part of the series is the middle 2 quarters of the year. You can easily change your program to only print the middle two quarters of the year by changing the observation range to 64-189 (252/4=63).

  • To make this change in your program, change the range of dateNums and price as shown below:
_images/first2.png
  • You should get a figure that looks like:
_images/first3.png
  • Suppose further that you now love the data selection on the graph, but you don’t like the unused space. You can fix this by adding the command “axis tight”. For more information, see: Axis Scaling and Appearance

Saving Ouput

  • You now love your data selection and your graph so much that you want to save it as a Portable Network Graphic file or ”.png”

  • First you need to save the figure as a variable. Place the command “fig1 = figure” before any plotting occurs (you could alternatively write fig1 = plot(x,y)). This action saves all the following figure commands to the variable fig1.

  • To save the final figure, you will need to add the command “saveas(fig1,’plotSP.png’)” at the end of all the figure commands. This saves the variable fig1 as a file named “plotSP” of type ”.png”.

  • Once you have done all this, run the program again. You can view the .png file in your MATLAB folder, which is located in your “Documents” folder on Windows 7.

  • Don’t forget to update your description of the program as your program no longer plots the entire year; only the middle two quarters.

  • Your final program should look like this:
_images/first4.png
  • Your final output should look like this:
_images/plotSP.png