Often you need to import data from an external source such as a .csv. See how to bring this type of data into the MATLAB workspace and manipulate it.
- [Instructor] Often we'll need to import external data into MATLAB from a file like an Excel spreadsheet or a comma separated value file. So let's see how to import a CSV. It's pretty simple. If you navigate to the exercise files folder, I provided a sine.csv file. And let's go ahead and import that. I'm going to copy it and paste it here into the current folder. Now we can use the function csvread to import it. So I'll create a variable called myData.
And then let's use the function csvread. And the only argument that it needs is a string that's the file name. So Sine.csv. Now keep in mind, if this file wasn't in the current folder, MATLAB wouldn't be able find it unless the folder that it was in is in the path. Let's put a semicolon here so we don't see all the data as it's imported. And we see myData created in the workspace. Let's double-click on myData. And we can see two columns of data. The first column is a straight line and the second column is a sine wave.
Just for fun, why don't we plot them? We haven't seen how to do plots yet, that'll be coming soon. But here's a little preview. We'll do plot myData and let's plot all rows, using a colon, of the first column. Go ahead and hit enter and we can see a nice straight line. Let's close that plot. Press up arrow, and I'll change from the first column to the second column. We hit enter here and we see a nice looking sine wave.
Reading in data from other external sources is very similar.
- Creating MATLAB variables
- Working with matrix and scalar operations
- Using if statements and loops
- Creating functions
- Importing data
- Building basic plots and 3D plots
- Working with images
- Creating Simulink models