How can I export a matrix as a CSV file?
조회 수: 3,072 (최근 30일)
이전 댓글 표시
Toby Feld
2016년 4월 26일
편집: MathWorks Support Team
2024년 11월 14일 5:50
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
jgg
2016년 4월 26일
Why do you want to export it like that? It seems really unnatural, and will be annoying to accomplish.
채택된 답변
MathWorks Support Team
2024년 11월 14일 0:00
편집: MathWorks Support Team
2024년 11월 14일 5:50
"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
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
2016년 4월 26일
a=[234,2
671,5
735,1]
csvwrite('file.csv',a)
댓글 수: 4
Bill Tubbs
2019년 7월 13일
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
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)
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.
댓글 수: 0
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:
csvwrite('test.txt',a.')
The '.' is important only if you have complex data
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import and Export에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!