Posted on

Learn Matlab Episode #10: Loading & Saving Data

Click here to subscribe for more videos like this!

How do you save & load data in MATLAB?

So in this tutorial I’m going to talk about loading and saving data. So a lot of the time when you’re doing data analysis your data is going to be passed around and files that you might download off the internet are stored on your hard drive or something like that, so you’re not going to be manually typing in matrices into matlab all the time. The first thing I want to talk about since it’s probably going to be really popular with Windows users is excel. So Excel uses a format called .xls and there are two functions in matlab that are useful for reading and writing .xls files. Ok, so if you type in help xls read you can see the documentation for the xls read function. There’s also a function called xls write that you may want to look into. So the reason I’m not going to cover xls read and write in this tutorial is because first of all it’s not perfect. So, I saved I don’t I’m on a mac so I used Google sheets to export and .xls document and it’s a very simple document, it’s just the three by three matrix with the numbers 123456789 in matlab. So xls read matlab example gives me an error when I’m trying to read this file, so we’re not going to be working with .xls. In addition to that, so if you’ve ever participated in a Kegel contest, or you get a file to work with from school, I’ve never seen anyone ever use .xls. Usually it’s a .csv, sometimes it’s a .mat file which is a matlab file. So we’re going to be working with those. So .csv I would say is the most common, it stands for comma-separated values, so your file will look something like this 123456789, and I’m going to save this CSV example .csv. So we have a function called csv read csv example, ok and so that did exactly what I wanted it to do. So you can see how the csv file pretty much one to one corresponds with the matrix that you would get after you read the file. Now let’s save a new matrix, let’s create something a little different. Ok, so just fives along the diagonal. I’m going to call csv write the another.CSV, so I did it backwards because I didn’t know. So I’m going to write the matrix B to a file called another.CSV. So if I look at this file…alright so when I double-click it matlab gives me kind of a CSV editor. So now what I’ve done is I’ve opened the same file in a text editor on the command line console and so you can see again what we’d pretty much expect a CSV of the matrix that we made. So that’s the nice thing about CSVs is they don’t just work with matlab they work with many other types of programs, it’s just a plain text file. So now the other way that we can read and write files is the .mat file which is matlab specific. So I’m going to clear my workspace, you’ll see why. I’m going to create one matrix A = [1,2,3,4], oops. Going to create another matrix B = [5,6,7,8] Ok, so now i have two matrices A and B so now what matlab lets you do is it allows you to save your entire workspace all into one file, and so all I do is I call save my workspace.mat, okay. So now if I look at this file, so I open this .mat file in a text editor, notice how it just shows me a bunch of garbage. So this is binary data specifically for matlab, so that’s why you might prefer to CSV so you can be cross-platform or cross program. So I’m going to clear my workspace again and I’m going to call load my workspace.mat and so what load does is it just reloads your entire workspace that you saved into the .mat, and so that could be useful if you don’t want to save every variable individually into their own CSV. What you can do is you can if you don’t want to save all the variables you can pick specific ones so let’s say I create another matrix C = [9,10,11,12] Ok, so I have variables A, B, and C but I only want to save A and C, so I’m gonna give it a file name AC.mat, I’m going to pass in so you can’t pass in the variable you have to pass in the name of the variable, so I’m going to pass in A and C to save it, I’m going to clear my workspace, and so now when I load AC.mat it just gives me back A and C only but not B.