Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The quickest way possible to save Matlab matrices

I am using Matlab to invoke some external call in C++ and then receive a huge calculated matrix back. The matrix is very huge, and I do not have access to this C++ program's source code. (if I have, I will make it save from C++ right away)

Right now, on my system, this C++ program only uses 1 second to calculate the given data and send back to Matlab, and Matlab's dlmwrite takes 200-300 seconds to save this single huge array on disk. I have some more thousands to compute, and I wanna cut the time down.

So what is the fastest way possible to save in Matlab?

like image 389
Karl Avatar asked Feb 22 '13 12:02

Karl


People also ask

How do I save a matrix as a .MAT-file?

Select MATLAB > General > MAT-Files and then choose a MAT-file save format option.

How do I save a large variable in MATLAB?

Use the matfile function to access MATLAB® variables directly from MAT-files on disk, without loading the full variables into memory. When you create a new file using matfile , the function creates a Version 7.3 MAT-file that also lets you save variables larger than 2 GB.

Why does MATLAB take so long to run?

A slow startup is often caused by issues with the license search path. You probably need to adjust one of the license environment variables (LM_LICENSE_FILE or MLM_LICENSE_FILE) used by MATLAB, or else bypass them all together.

How do I save a set of data in MATLAB?

To save variables to a MATLAB script, click the Save Workspace button or select the Save As option, and in the Save As window, set the Save as type option to MATLAB Script. Variables that cannot be saved to a script are saved to a MAT-file with the same name as that of the script.


1 Answers

The fastest way possible is probably Matlab's save command. Alternatively, you could fwrite the whole matrix to a binary file.

Using dlmwrite converts the values to text, which takes time and is more data to write to disk. Don't do that unless you really need to have the data in that format. Note that dlmwrite will be faster if called one time with a big matrix instead of in a loop that incrementally writes your file.

like image 140
shoelzer Avatar answered Oct 31 '22 13:10

shoelzer