Reputation: 5733
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?
Upvotes: 5
Views: 6331
Reputation: 574
If you want a Matlab only solution, I would probably use fwrite for binary or fprintf for ASCII. However, I like to mix Matlab and Java when it comes to IO since this is usually faster. I would do something like
Java Code
package mypackage.release;
import java.io.DataOutputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException
public class MatrixWriter {
public static void write(String fileName, double[] matrix) throws IOException {
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(fileName)));
for (double d : matrix)
dos.writeDouble(d);
dos.close();
}
}
Matlab Code
import mypackage.release.MatrixWriter;
M = get matrix from c++;
MatrixWriter.write('myfile.dat', M(:));
This is something off the top of my head, but I use variations of this all the time. Hope it helps.
Upvotes: 2
Reputation: 10708
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.
Upvotes: 5