How can I export a matrix as a CSV file?

Hi, 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 csvwrite('test.txt',a) I get the following: 234,2671,5735,1264,2346,7 What is going on here? How can I solve this issue? Thanks a lot, Toby

2 commentaires

jgg
jgg le 26 Avr 2016
Why do you want to export it like that? It seems really unnatural, and will be annoying to accomplish.
Ian Mclennan
Ian Mclennan le 15 Juil 2019
This thread really helped me, thank you for posting.

Connectez-vous pour commenter.

 Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 14 Nov 2024
Modifié(e) : MathWorks Support Team le 14 Nov 2024

19 votes

"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] writematrix(m,'M.csv')

4 commentaires

Trevon McHansen
Trevon McHansen le 29 Jan 2021
To potentially save others an extra click or two, "writematrix" was introduced in R2019a.
Larry Gulliver
Larry Gulliver le 18 Juil 2022
Thank you!
Reem
Reem le 5 Oct 2022
After I do that, how can I see the csv file?
Chris
Chris le 2 Nov 2022
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.

Connectez-vous pour commenter.

Plus de réponses (4)

Azzi Abdelmalek
Azzi Abdelmalek le 26 Avr 2016
a=[234,2
671,5
735,1]
csvwrite('file.csv',a)

4 commentaires

Shuva Paul
Shuva Paul le 29 Avr 2019
Great, thanks for the solution :)
Mehdi BENSEDDIK
Mehdi BENSEDDIK le 21 Mai 2019
I appreciate you for the sharing
Being a Python user coming to Matlab I was a bit shocked to find that csvwrite truncates the data output.
csvwrite('u.csv', u)
File contents:
1000,957.6,917,878.13,840.9, ...
Then I happened to check the documentation and found this:
csvwrite is not recommended. Use writematrix instead. For more information, see Compatibility Considerations.
writematrix(u)
File contents:
1000,957.603280698574,917.004043204671,878.12608018665,840.896415253715, ...
Guillaume
Guillaume le 13 Juil 2019
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).

Connectez-vous pour commenter.

Jos (10584)
Jos (10584) le 26 Avr 2016

2 votes

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
Sindar le 12 Mar 2019
It sounds like all you need to do is print a column vector instead of a row vector. This can be done easily:
csvwrite('test.txt',a.')
The '.' is important only if you have complex data
Toby Feld
Toby Feld le 26 Avr 2016
Modifié(e) : Toby Feld le 26 Avr 2016

1 vote

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.

Catégories

En savoir plus sur Data Import and Export dans Centre d'aide et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by