lettura simple

Saving Data to a File in Matlab

Let me explain to you how you can save data in a file using Matlab, and how you can load it back into memory.

Why save data? When you exit Matlab, the memory where your workspace was stored in the RAM gets reset. By saving your workspace data in a file on your hard disk, you can reload it back into memory when you come back to Matlab. This way, you can avoid typing everything again.

How to save data to a file

Here's a practical example.

Say you have three arrays named M1, M2, and M3. You create them in memory using these commands:

>> M1=[1 2 3; 4 5 6];
>> M2=[7 8 ; 9 1];
>> M3 = [ 3 4; 5 6; 8 9];

To save all the data in your workspace, use the command "save" followed by the name of the file you want to use, like this:

>> save filename.mat

Now, clear the workspace data using the "clear" command, or simply exit and re-enter Matlab.

>> clear

This will delete all the data in your memory, and the Matlab workspace will be reset. If you try to use the matrix M1, Matlab won't find it and will give you an error.

>> M1
Unrecognized function or variable 'M1'.

To load the data back into memory, use the "load" command followed by the name of the file:

>> load filename.mat

This command will load the data from the file into your workspace.

If you type "M1" again, Matlab will find it in memory and show you its content.

>> M1
M1 =
1 2 3
4 5 6

Note. In these examples you saved a matrix with numbers. In Matlab, you can save data of all types: numbers, strings, characters, images, etc.

How to save specific objects

You can also save only specific objects in your workspace. To do this, you need to specify which objects you want to save, like this:

>> save filename2.mat M1

This will save only the matrix M1 in the file "filename2.mat".

If you want to save both M1 and M2, you can use this syntax:

>> save filename3.mat M1 M2

Alternatively, you can use this syntax:

>> save("filename3.mat", "M1", "M2")

Note. To add other objects to the data save, add them at the end. For example, if you want to save all three objects, type save filename3.mat M1 M2 M3 or save("filename3.mat", "M1", "M2", "M3").

You can also save all objects that start with a specific letter using the * special character.

For example, to save objects that start with the letter M, type save filename3.mat M*.

>> save filename3.mat M*

To load the data into memory, use the load command:

load nomefile2.mat

How to load a single object into memory

Well, you see, when you have a large number of objects stored in a file, it may happen that you only need to use one of them.

In Matlab, you have the option to selectively load the object you need without loading all of them.

And why is this important, you may ask? The reason is that the data takes up computer memory space, and there's no need to load data that you won't be using. By loading only the data you need, you commit a smaller amount of RAM and the computer can work more quickly.

To load only one object, simply indicate the object name after the file name.

For instance, if you saved matrices M1, M2, and M3 in the file named "filename.mat," to load only the M1 matrix into memory, you would type load filename.mat M1 on the command line.

>> load filename.mat M1

Matlab opens the file and loads only the M1 matrix into memory, ignoring the other matrices in the file.

To load M1 and M2, you would type load filename.mat M1 M2 on the command line.

>> load filename.mat M1 M2

In this case, Matlab loads the M1 and M2 matrices into memory but not the M3 matrix.

Alternatively, you can use the following load syntax, which produces the same result: "load('filename.mat','M1','M2')".

>> load("filename.mat","M1","M2")

Note that to load three or more objects into memory, you would add the objects to the end of the list. For example, to load three matrices, you would type load filename.mat M1 M2 M3.

You can also load all objects that start with a certain letter.

For example, to load objects that start with the letter M, you would type load filename.mat M*

>> load filename.mat M*

How to save data in a binary file

Matlab also allows you to save data in a file using the binary format.

To save data in binary, use the "save" instruction with the "-mat" option.

>> save -mat filename4.mat

To load the data present in a binary file into memory, instead, use the "load" instruction.

>> load nomefile4.mat

What is the purpose of saving data in binary?

Binary format is very useful when you want to process data saved in Matlab with other software.

Other saving options

Additionally, the "save" command in Matlab has several useful options, such as

  • -append
    to add data to an existing file without deleting existing data
  • -nocompression
    to save data without compression
  • -ascii
    to save data in ASCII text format
  • -mat
    to save data in binary format
  • -struct
    to save data from a structure.

To use multiple options in the "save" command, use this syntax: "save('-option1', ..., 'file', 'v1', ...)".

save ("-option1", ..., "file", "v1", ...)

If you want to know all the options for the "save" command, type "help save" on the command line.

>> help save




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin