Today we will be covering the following topics:
1. Importing Files into MATLAB
2. Operations with Imported Data
3. Exporting Files from MATLAB
4. Viewing Contents of a MATLAB File Before Opening
video coming soon!
Importing Files into MATLAB
There are ways to import data from other file extensions into MATLAB to use as datasets. Note that when you import files into MATLAB, any other variables in your workspace with the same name as variables within the imported file will be overwritten. Here are some examples using various different methods of importing data.
Example – Loading Data from Import Data
Go to the Home tab, then Variable, and click Import Data. There will be a pop-up window where you can browse for a file to open from your computer, and then select specific data to import if you don’t want all the data from the file.
I am going to select Book1.xlsx (the very first file) to be loaded. You can browse your computer and select the file from anywhere. For some of the other commands though, the file should be in your MATLAB folder in order for it to work. So I recommend to just keep all your MATLAB related files in the MATLAB folder to begin with, so you don’t have a problem loading any data.
The following image is what comes up when I double click Book1.xlsx. The columns A, B, and C are the same columns MATLAB is reading from the excel sheet. Hit the Import Selection button on the top right to import the data into the MATLAB workspace. There won’t be any pop-up when you click this, but it will be imported in the background.
If you now go to the MATLAB workspace, the columns of the excel sheet will be there:
Example – Loading Data with uiimport Command
Use the command uiimport, either in a script or directly in the Command Window. The syntax is uiimport(filename), where you will replace the word filename with the name of the file you wish to import. This command can be used with an image, a spreadsheet, and other file types. Let’s try importing the same file as before – Book1.xlsx. To do this, we need to put the name of the file in single quotes, with the extension xlsx attached: ‘Book1.xlsx‘ and then we place this within the uiimport brackets:
As before, the same window to import the columns pops up:
When you select Import Selection from the top right (in the image above), you will notice that the variables are in the workspace:
Example – Loading Data with importdata Command
This command does not help if you want to load and use data from a spreadsheet – it stores the data as an array. But you can use it to load images, for example. Let’s load a .jpg image:
Now we need to add another line in order to turn the imported data into an image. Use the syntax image(variable), where the word variable is replaced with the name of the variable that stores the imported data. My data was stored in the default variable ans, so I use image(ans):
Then the photo comes up:
Example – Loading Data with xlsread Command
Another way to import data is using the xlsread command. This works with the same syntax as the image above, but replace the word uiimport with xlsread. Although MATLAB does not recommend to use xlsread due to compatibility issues (it recommends to use readtable instead, which I discuss below), it is a command specifically for loading spreadsheets. This is the syntax to use, and you will replace the name of your file within the brackets, as I have used Book1.xlsx.
Example – Loading Data with load Command
This is usually only used for .mat extension files. I have a file called Book1.mat, and I will type the following syntax to load it:
It will then appear in the Workspace:
Example – Loading Data from the Current Folder
To load variables from a MATLAB file into the workspace using the Current Folder, you will need to start by typing the words filebrowser into the Command Window and hitting the Enter key. Once you do that, the Current Folder will pop up on the left side of the screen, and you can select your file to open from there.
You will get the same pop-up to import the data as you did using uiimport or using the Import Data button, so I won’t paste the same screenshot again below. The variables will then appear in your workspace, just like with the previous examples.
So now that we know how to load data, how do we load it as different data types? Let’s check out some examples. You can load variables as either column vectors, a table, cell array, or numeric matrix. Let’s look at some examples.
Example – Loading Data as a Cell Array
Readcell will read data from the following file extensions: .txt, .dat, .csv, .xls, .xlsb, .xlsm, .xlsx, .xltm, .xltx, .ods.
You can either import the data using readcell:
or using the Import Data button near the top of the screen:
When the pop-up window comes, select Cell Array. Then hit the Import Selection button at the top right.
If you check the Workspace, the data should be imported as a cell type.
Example – Loading Data as a Table
To load data from a file as a table, you can either select “Table” using the Import Data tab like we did with a Cell Array, or you can use the command readtable with the filename next to it (which is the method I will use here). This command reads the first row of the file as the variable names (usually you would have titles for each column in the first row). This is the syntax I would use if I wanted to load the data from the same file I used above – Book1.xlsx:
This is what would show in the output. The columns from the excel file are now shown (and stored) as a table in MATLAB:
Operations with Imported Data
Well, now that we have imported data (using any one of the various methods) – how do we actually use it? Let’s look at some examples:
Example – Extract a specific column of the dataset and find the average
This can be done in two ways. You can select the Import Data button first
Then select the column or columns you would like. I have used only B2:D4, but by default it would have highlighted over all the columns until you specify. Then you will select Import Selection from the top right of the screen:
Or you could just import all of it, and then specify from the Workspace which variables you want to use. If I did import all the data, this is what my Workspace would look like (with the two variables Name and Height):
If I only wanted to find the average of the Height (the second column) , I could do that by using the mean command and then specifying that variable (Heightinches).
There are other operations that can be done, but the average was just one example.
You can also read the data and then specify the rows and columns you want to extract. Say I use readtable to get all of the data from Book1.xlsx into a table, and I store it in a variable newtable.
This is the table that is extracted:
Now we can specify the row and column, or multiple rows and columns, we want to extract. Say we want the 2nd row, 2nd column. We will call that variable newtable(2,2). This has extracted the value from the specified row and column:
Now if we want to extract the 2nd column with all the rows, we use a colon in the place where you’d specify the number of rows (to indicate we want all of them – this was discussed in a previous lesson), and then use a 2 where you’d specify the number of columns:
Exporting Files from MATLAB
We can use a few different commands to export data from MATLAB into a file on your computer: xlswrite, writetable, writematrix, writecell, and save are some of the main ones. Note that xlswrite is not recommended by Mathworks for possible compatibility issues.
Example – Exporting Data to Spreadsheet with save
To use this command, you must export the file extension as a .mat. Here I have created two vectors A and B.
Now when I export these to a file, I would like to call this exported file new. This is the syntax I would use. save(filename, variable 1, variable 2, …). Each argument in the brackets must be in single quotes:
If you check in the MATLAB folder, there will now be a file with that name:
To use this file, you can open it up in MATLAB and it will load all the variables in this file, right into your MATLAB Workspace.
Example – Exporting Data to Spreadsheet with writetable
The syntax is writetable(variable, file name and extension). Here I have made two vectors, Height and Age.
I will first have to create a table to use writetable. This is the syntax table(variable 1, variable 2,…). I only had the two variables Height and Age to use:
Now to write these to a .xls file, use writetable(table variable, file name and extension). Replace the words table variable with the variable your table is stored in – that is variable T for me. Then replace the words file name and extension with the file name and extension you want to export the table as – I used heightage.xls, because I wanted the file to be called heightage, and the extension to be an Excel spreadsheet, .xls.
Running this code will create an Excel file in the default MATLAB folder – if you open your folder, you will notice the new file:
Although I only covered specific examples for data types to import and export, below is a list of supported file types for both importing and exporting commands:
Viewing Contents of a MATLAB File Before Opening
To view what a file may contain before opening, type in the words filebrowser to the Command Window.
Hit the Enter key, and this is what will pop up:
Then right click on any of the .m files and select Show Details.
This is what it will look like, with a little window on the bottom left popping up (called the Details Panel). You can go up and down through your files (these are the ones saved to the MATLAB folder) and the details of each will come up in that window. You can see in my file untitled5.m it shows a little explanation of what I had typed in that file. To open the files into MATLAB, you can double click them.