This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

csvwrite

Write comma-separated value file

Syntax

csvwrite(filename,M)
csvwrite(filename,M,row,col)

Description

example

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.

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.

Introduced before R2006a