Importing and Exporting

You should now be familiar with how MATLAB functions as a software package. The next step would be to look at how to import or to export data into MATLAB. We have had a very limited look at importing/exporting variables from the workspace in the previous section, but this section aims to give you a more complete understanding.

 

2.3.1 Importing (or Reading in) Data

You might be wondering why one would need to import data. For example, in order to plot graphs, you need data. You can either input the data manually into MATLAB, or you can get it to import external data for use in its workspace. In order to import data, it needs to be laid out in a suitable way for MATLAB to be able to store it. Files such as .txt files and .csv files are appropriate here (there are many to choose from).

There are a few methods of importing data. The importation process extracts external data from external files, assigns them to variables (that you will need to specify) and stores them into the workspace of your current MATLAB session. The workspace is stored in MATLAB’s RAM and the variables stored into it are therefore only temporary; if you shut down MATLAB and reopen it, all your variables will be gone. That’s why you always need to create a script, which you should save regularly, in order to avoid data loss.

This implicitly means that if you import data during one MATLAB session, you will lose all the workspace values that you have imported once you end your session. To avoid this, it is always best to incorporate your importation code into a controlling script file that you can execute once you reopen MATLAB to avoid you having to go through the sometimes tedious process of importing large sets of data.

Since MATLAB is matrix based (even an integer is stored into a 1×1 matrix), arrays in MATLAB are very similar to matrices. You can perform operations on or using those arrays, such as MATRIX operations and indexing (searching).

When importing data, you are therefore effectively creating a matrix, or a set of matrices. MATLAB is so powerful that you can even create a matrix within a matrix, or a matrix from a matrix, and still perform operations on them, but more on that later in the Creating a Matrix from a Matrix part of the Graph Plotting section.

To get started with importing data, you will need to first prepare it for importation, otherwise MATLAB won’t understand what you’re trying to get it to read in. This is best explained by proving an example.

 

Example 2.2
Suppose that you wanted to import a set of data from a text file that was created using a data logger. The text file contains two columns, of time, t, and vertical displacement, z, as shown below (with sample data).

Displacement at certain times
t(s) z (m)
0.0   1.000
0.1   0.955
0.2   0.900
…     …
…     …
10.0  0.270

 

As can be seen, the data contained within this file is uniform and is consistent throughout. Therefore, not much manipulation is required in this case and all that is required is to do is to make the data importable by MATLAB. This is done by changing the text file to display what is shown below. The bits in red are the adjustments that were needed to make the data readable by MATLAB.

% Displacement at certain times
% t(s) z (m)
t_z = [0.0   1.000
0.1   0.955
0.2   0.900
…     …
…     …
10.0  0.270];

 

Let’s take a look at what the additions mean:

  • The % comments out the titles
  • t_z is the variable name that MATLAB will assign the imported data to.
  • The [ square bracket opens up the matrix of two columns.
  • The ] square bracket closes the matrix

Once you have modified the text file, save it as a new file with a .m extension, as shown in Figure 2.1. The “…” rows have been neglected because they were only included above to simplify large sets of data.

Figure 2.1

Figure 2.1

 

If we now called this M-file into MATLAB, notice what happens to the workspace. In the command window, type in ‘data’ without the speech marks to call it the file in MATLAB.

Hint: You must make sure that the file is in your working directory (current folder). Refer to the earlier tutorial on how to set up or change your working directory!

You should end up with the variable t_z introduced into the workspace, as shown in Figure 2.2.

Figure 2.2

Figure 2.2

 

Upon inspection, you may realise that double clicking t_z in the workspace, or calling it in the command window, should show you the same data as that in the text file. You can then go on to change the data by modifying t_z in MATLAB, but remember that it will be stored in MATLAB’s temporary memory, so to make any change permanent, you would need to either modify the M-file (by opening it within MATLAB and overwriting the existing one), or create a new M-file that contains all your commands (so that MATLAB processes data without actually changing it).

The data will import as a matrix. You can tell MATLAB to show you the dimensions of each of your matrices (variables) by right clicking in the workspace toolbar and by checking ‘Size’ in the context menu, as shown in Figure 2.3.

Figure 2.3

Figure 2.3

 

MATLAB will then show your matrix sizes in the workspace! See Figure 2.4.

Figure 2.4

Figure 2.4

 

And there you go! The variable t_z, which was introduced within the text file, has just been imported into MATLAB, and you may now perform operations on/using it.

There is another way of importing files in MATLAB. You may wish to use the built in importer, ‘Import Data’, which can be found under the ‘Home’ tab in the ‘Variable’ section, as shown by Figure 2.5.

Figure 2.5

Figure 2.5

 

If you already have a matrix declared in an M-file, you may import it by simply calling the M-file!

 

2.3.2 Exporting Data

Exporting is the opposite of importing; it is the act of transferring data out of the system. Exporting can be used to back up your data and to use your data in other software packages (in formats that are compatible between the two). For example, saving the workspace is the same as exporting it. Another example would be saving scripts as different formats: if you saved a script as a text file when it was initially an M-file, then that would be an export.