Image via Wikipedia
There are many different ways to export and import data from matlab. You can import and export data from and to matlab binary formats (MAT files), text files, Excel spreadsheet (works only on Windows), XML, several special purpose formats, and a lot of image, audio, and video file formats. Text formats are very useful, because they very portable in that they can be read and written by many different applications.
In this post I will give some examples of exporting to different text formats. I will also mention how to import data using complementary commands.
The simplest way to export data to text format is this:
Matlab exports data by default in the scientific numeric format. If you want to use these data with some other program outside matlab, this can lead to problems. Although nowadays many programs use libraries that permit reading scientific notation (e.g. boost regex library for C++), it is sometimes better to write to a fixed-digits format.
-double option says that you want the numbers in 16-digit format.
To write matrix A to a column separated value (CSV) file, there are several alternatives.
dlmwrite is one possibility:
The default delimiter is already the comma, so the last parameter is unnecessary. If you want your data space-separated this command is your friend:
If you use the tabulator as delimiter, you can use also use
The last option I give here is
These commands work for vectors and two-dimensional matrices.
Also sometimes useful is
diaryto save your command history to a disk file. You can view and edit the resulting text file using any word processor.
For importing data to matlab you can use the corresponding commands dlmread, load, csvwrite. Some files you might have to filter before reading them into matlab. For example to get rid of comments. Say the files provide comments at the start of the line starting with the percent sign (%). Then filtering can be done with sed:
sed -i /^%/d *
Enjoy. Please leave a comment below for questions and suggestions.
You might also be interested in my article on exporting figures from matlab. I also wrote an article about creating videos in matlab.