How can I export a matrix as a CSV file?

5,170 次查看(过去 30 天)
I have a 320x2 matrix with doubles. I want to save it in a CSV file in the following pattern:
234,2 671,5 735,1 264,2 346,7 ... etc.
I googled this problem and found the function "csvwrite". However, when I use that function like this
I get the following:
What is going on here? How can I solve this issue?
Thanks a lot, Toby
  2 个评论
Ian Mclennan
Ian Mclennan 2019-7-15
This thread really helped me, thank you for posting.



MathWorks Support Team
"csvwrite" is not recommended. Use "writematrix" to export matrix data as a CSV file instead.
When writing matrix data to a file, you can specify the file type as part of the file name in the second argument of the function call:
m = [234 2;671 5;735 1;264 2;346 7]
  4 个评论
Chris 2022-11-2
Look at your file hierarchy on the left you'll see a file that's been generated with the name you gave it. You can right click and show it in explorer.


更多回答(4 个)

Azzi Abdelmalek
Azzi Abdelmalek 2016-4-26
  4 个评论
Guillaume 2019-7-13
writematrix is fairly new and did not exist when this question was ask.
I was a bit shocked to find that csvwrite truncates the data output.
Any conversion of number to text will truncate the representation of most numbers, in python as well. As documented csvwrite has a precision of 5 digits. For more precision, the alternative at the time was dlmwrite. Interestingly writematrix also doesn't have an option of altering the precision and always use longg (15 digits of precision).


Jos (10584)
Jos (10584) 2016-4-26
You and your computer disagree on what to use as a decimal symbol: a . or a ,
I strongly recommend you to use a decimal point. Probably that means you have to change your locale / regional settings in the operating system.

Sindar 2019-3-12
It sounds like all you need to do is print a column vector instead of a row vector. This can be done easily:
The '.' is important only if you have complex data

Toby Feld
Toby Feld 2016-4-26
编辑:Toby Feld 2016-4-26
For some reason the example I used was re-edited by the forum software to let it appear in one line. I cannot figure out how to disable that formatting (seriously, WTF??). So here is how I want the CSV file to look as a png file. I hope the forum does not change graphic files.


Find more on Data Import and Export in Help Center and File Exchange


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by