Simple Way of Writing and Reading files in Matlab | applied electronics engineering

# Simple Way of Writing and Reading files in Matlab

By Applied Electronics - Thursday, February 16, 2017 No Comments
Writing and Reading a file using Matlab is essential thing to learn. You might want to save your data for future purpose, for further manipulation or for presenting them in some report. Similarly you might want to read data from different file source and manipulate them in matlab.

There are lots of matlab commands and functions that allows you to save data and import data from different file formats. Here is a simple way of writing and reading files in Matlab. We will use the save and load matlab functions to write and read data. These simple commands allows you to save the data in a file with with extension .txt or .dat and others.

### Simple way of writing to a file using save

To write to a file, that is, save your data in a file the simple way is to use save command. The syntax to use save command is as follows:

save <filename> <matlab_variable> -<qualifier>

The <filename> is the name of your file. This name is upto you. The <matlab_variable> is the variable or you data that you want to export. A hypen then follows followed by <qualifier>. The <qualifier> tells other associated commands to be used with save command. For example if you want to save the variable in ascii character format the qualifier is then ascii.

For example:

save myfile.txt myvar -ascii

where myfile.txt is your filename that you gave, myvar is the matlab variable which can be anything and lastly the ascii as your qualifier.

Important note: Note that there should be no space between the hypen(-) and the qualifier ascii other you will get error message - "Unknown command option."

Suppose your variable is a matrix stored in variable myvar then you would save the matrix as follows:

>> myvar = rand(2,3)

myvar =

0.8147    0.1270    0.6324
0.9058    0.9134    0.0975
>> save myfile.txt myvar -ascii


To view the content of the file or you data back in matlab you would use the type command. For example,

type myfile.txt

would display the content of myfile.txt which is the matrix that was saved.

>> type myfile.txt

8.1472369e-01   1.2698682e-01   6.3235925e-01
9.0579194e-01   9.1337586e-01   9.7540405e-02

#### Appending data to your saved file

Now what if you want to other data in the same file such as myfile.txt in the example above. Now we will come to another example of qualifier. The append is another qualifier which you can use to add new data into your existing file. For example suppose you created another matrix say myvar2 then you can add this variable in the same file myfile.txt using the append qualifier as follows:

>> myvar1 = rand(2,3)

myvar1 =

0.2785    0.9575    0.1576
0.5469    0.9649    0.9706

>> save myfile.txt myvar -ascii -append
>> type myfile.txt

8.1472369e-01   1.2698682e-01   6.3235925e-01
9.0579194e-01   9.1337586e-01   9.7540405e-02
8.1472369e-01   1.2698682e-01   6.3235925e-01
9.0579194e-01   9.1337586e-01   9.7540405e-02

The simple way of reading file in Matlab is to use the load command. This will load the data saved in a file into matlab workspace memory so that you can manipulate or work on the imported data. The type command used in the pervious example only allows you to view the data but you cannot do anything with them.

The syntax to use load command is simple:

So for example, we wished to load the data saved earlier in file myfile.txt. To do this we write as follows:

>> load myfile.txt
>> myfile

myfile =

0.8147    0.1270    0.6324
0.9058    0.9134    0.0975
0.8147    0.1270    0.6324
0.9058    0.9134    0.0975

By typing the filename(without the extension) the content of the file is displayed.

Once you have read the data into matlab workspace using the load command you can then work on the data as desired. Like for example you want to save the first row of the file data as a vector v. You can do that in the following way:

>> v = myfile(1,:)

v =

0.8147    0.1270    0.6324

So this tutorial basically showed you the simple way of writing and reading files in Matlab Software. This is thus for reading text files. Matlab also supports reading and saving audio files. See matlab audioread and audiowrite examples to learn how to work with audio files.