Main Content

csvwrite

(Not recommended) Write comma-separated value file

csvwrite is not recommended. Use writematrix instead. For more information, see Compatibility Considerations.

Description

csvwrite(filename,M) writes matrix M to file filename as comma-separated values.

example

csvwrite(filename,M,row,col) writes matrix M to file filename starting at the specified row and column offset. The row and column arguments are zero based, so that row=0 and col=0 specify the first value in the file.

example

Examples

collapse all

Create an array of sample data M.

M = magic(3)
M = 3×3

     8     1     6
     3     5     7
     4     9     2

Write matrix M to the file 'myFile.txt'.

csvwrite('myFile.txt',M)

View the data in the file.

type('myFile.txt')
8,1,6
3,5,7
4,9,2

Write a matrix to a file starting at a defined offset position.

Create an array of sample data M.

M = magic(3)
M = 3×3

     8     1     6
     3     5     7
     4     9     2

Define the starting offsets to skip one row and two columns.

row = 1 ;
col = 2 ;

Write matrix M to the file 'myFile.txt', starting at the offset position.

csvwrite('myFile.txt',M,row,col)

View the data in the file.

type('myFile.txt')
,,,,
,,8,1,6
,,3,5,7
,,4,9,2

Input Arguments

collapse all

File name, specified as a character vector or string.

Example: 'myFile.dat'

Data Types: char | string

Numeric data to write, specified as a matrix of numeric values.

Example: [1,2,3;4,5,6]

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
Complex Number Support: Yes

Row offset, specified as a scalar. The row offset indicates the number of rows to skip before writing the numeric data. row is zero-based, so that row = 0 instructs MATLAB® to begin writing in the first row of the destination file. Skipped rows are populated with commas.

Column offset, specified as a scalar. The column offset indicates the number of columns to skip before writing the numeric data. col is zero-based, so that col = 0 instructs MATLAB to begin writing in the first column of the destination file. Skipped columns are separated by commas.

Limitations

  • csvwrite writes a maximum of five significant digits. If you need greater precision, use dlmwrite with a precision argument.

  • csvwrite does not accept cell arrays for the input matrix M. To export a cell array that contains only numeric data, use cell2mat to convert the cell array to a numeric matrix before calling csvwrite.

Algorithms

  • csvwrite terminates each line with a line feed character ('\n' or char(10)) and no carriage return.

Version History

Introduced before R2006a

collapse all

R2019a: csvwrite is not recommended

csvwrite is not recommended. Use writematrix instead. There are no plans to remove csvwrite.

Starting in R2019a, use the writematrix function to write a matrix to a comma separated text file. The writematrix function has better cross-platform support and performance over the csvwrite function.

This table shows typical usages of csvwrite and how to update your code to use writematrix instead.

Not Recommended

Recommended

csvwrite('mydata.txt',M)
writematrix(M,'mydata.txt')

Go to top of page