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:
save -ascii
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.
Using save, the -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:
>> dlmwrite('attr20.ascii',A,'delimiter',',');
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:
>> dlmwrite('attr20.ascii',A,'delimiter','\t');
If you use the tabulator as delimiter, you can use also use save:
>> save('attr20.ascii','A','-ascii','-double','-tabs');The last option I give here is
csvwrite:>> csvwrite('attr20.ascii',A); These commands work for vectors and two-dimensional matrices.
Also sometimes useful is
diary to 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.

so this is the way to do that, my classmate commented something about this function, but he can't explain so well as you, and for this reason I has been for hours looking for a good answer.
Hi,
this has worked great but is there anyway to get it to write the values on a new line each time rather than in a long line??
@James: I suppose you want line breaks as delimiters. Try dlmwrite('attr20.ascii',A,'delimiter','\n');
Thank u very much for the article. Could you plz kindly guide me how to change the default format(scientifice notation) in csvwrite()
Hi,
this has worked great but is there anyway to get it to write the
values on a new line each time rather than in a long line??
With dlmwrite, try \n or \r as delimiter, for example like this: dlmwrite('attr20.ascii',A,'delimiter',',');
Use the precision argument. For example, if you want to have 10 digits before and 5 digits after the decimal point: dlmwrite('attr20.ascii',A,'precision','%10.5f');
I am very interested by reading this..Its not a common thing to post a such article..I appreciate your blog..Keep posting this type of articles..esire to learn even more issues approximately it!
Nice to read your blog on this website. different experience
to read this blog. i get some awesome information throw this article.
I admire your dedication to
excellence. Your content is very obviously well-researched. I got so involved
in your article that I couldn’t even make myself quit reading. Thank you for
producing such great quality work.
site: http://exerciseprograms.co/
Good post, thanks for sharing it!
I’m extremely pleased to read through this original content!
Your site appeared to be extremely completely unique and possibly a must read!
Compared with other posts, your site was full of subject matter which is
fascinating to see! Hope you like my quick remark. Keep publishing!
Otsingumootori
optimeerimine
It was amazing visiting your blog. This is kind of different than other blogs. Marietta
bankruptcy attorney emagrecer Idol
White
Subscribe to replies to this post
This conversation is missing your voice. Your feedback is appreciated.
Post a Comment
You can use some HTML tags, such as <b>, <i>, <a>
You can follow the discussion of this post by subscribing.
You are free to include information from this article on your own site if you provide a backlink. You can use the following markup: