Join Patrick Royal for an in-depth discussion in this video Creating scripts, part of Learning MATLAB.
Let's practice writing simple scripts in MATLAB by creating a program that provides some summary statistics for a list of daily prices for the SMP 500 from 2000 to 2010. The sample data can be found in the excersize files for chapter three. To start with, click the New Script button to open up the script editor window and save it as practice scripts.m. Here you can write the relevant commands an run the script.
The first thing we need to do is to load the chosen data into the function. It can't directly reference a file in its formulas. I won't go into a lot of detail about file formats in this video but MATLAB can automatically convert CSV files into matrices. So you can simply use the CSV read function. I'll start by defining a new variable called stocks. And set it equal to CSV read of sample data.csv.
The data in this file has only one column containing the clothes prices so the matrix should be 2515 by 1. If you run the script that is exactly what you will see. From here manipulating the data is as simple as calling the appropriate functions. An easy one to start out with is finding the mean. So we could write mean of stocks. If you just want the output to go to the command window you don't need a separate function. Instead you can just leave off the semicolon at the end of the line and the result will automatically display. So when we run this in the command window we can see that we have the mean here. Scripts can also contain more complicated formulas.
For instance if you wanted to split the data by year and then display each of those means, you could use a for loop as follows. There are 10 years. So the first line would be for I equals 0 to 9. i is simply a dummy variable used to keep track of the loop counter. The next line needs to calculate the mean of just 1 10th of the matrix. To reference just that part you can say, stocks of 250 times I plus 1 to 250 times i plus 250.
This formula might look a bit confusing but it basically just tells the program to start at the given year where each year is 250 business days and then only consider the next 250 values. You can then surround this with a mean function as before to get mean of stocks of the current day to 250 days in the future. Again leave off the semi colon to insure that the output displays. From here all you have to do is end the loop and run the script to see the results.
These are the basics of creating a script. You can do anything that I've covered in previous videos in a script and it will run exactly as if it were on the command window. The advantages of a script are twofold. First, complicated loops, conditional statements and so on are much simpler to work with if you have multiple lines and plenty of space. Second, whatever you type into a script so that you can refer back to it and run it again whenever you need to.
- Installing MATLAB
- Working with MATLAB variables
- Working with matrix and scalar operations
- Creating functions
- Understanding performance considerations
- Building basic plots
- Creating responsive programs
- Editing variables manually
- Working with the Statistics Toolbox