Main Content

writelines

Write text to file

Since R2022a

Description

example

writelines(lines,filename) writes the text specified by lines to a plain text file named filename. If the file specified by filename does not exist, writelines creates a new file. Otherwise, writelines overwrites the existing file.

example

writelines(lines,filename,Name=Value) specifies options using one or more name-value arguments. For example, you can append the text to an existing file, specify end-of-line characters, or specify the character encoding scheme.

Examples

collapse all

Write the text "Example String" to a new file within the current directory.

writelines("Example String","temp.txt")

Display the contents of the new file.

type temp.txt
Example String

Append a string to an existing file.

lines = "New Content 456";
filename = "C:\Users\asato3\Desktop\original_file.txt";
writelines(lines,filename,WriteMode="append")

Display the contents of the appended file.

type C:\Users\asato3\Desktop\original_file.txt
Original Content 123
New Content 456

Input Arguments

collapse all

Text to write, specified as a string array, character vector, or cell array of character vectors. Each element of the array is written as a separate line in the file.

Example: "Sample text"

Example: ["String1","String2","String3"]

File name, specified as a string scalar or character vector, used to designate where to write the lines of data. Depending on the location of the file, filename can take on one of these forms.

Location

Form

Current folder or folder on the MATLAB® path

Specify the name of the file in filename.

Example: "myFile.txt"

File in a folder

If the file is not in the current folder or in a folder on the MATLAB path, then specify the full or relative path name in filename.

Example: "C:\myFolder\myFile.xlsx"

Example: "dataDir\myFile.txt"

Internet URL

If the file is specified as an internet uniform resource locator (URL), then filename must contain the protocol type "http://" or "https://".

Example: "http://hostname/path_to_file/my_data.csv"

Remote Location

If the file is stored at a remote location, then filename must contain the full path of the file specified with the form:

scheme_name://path_to_file/my_file.ext

Based on the remote location, scheme_name can be one of the values in this table.

Remote Locationscheme_name
Amazon S3™s3
Azure® Blob Storagewasb, wasbs
HDFS™hdfs

For more information, see Work with Remote Data.

Example: "s3://bucketname/path_to_file/my_file.csv"

Example: writelines("Sample string","temp.txt") writes to a file in the current folder.

Example: writelines("Sample string","s3://bucketname/path_to_file/temp.txt") writes to a file at the Amazon S3 URL.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: writelines(lines,filename,WriteMode="append") appends data to an existing file.

End-of-line characters, specified as either a string scalar or character vector. The default value is dependent on system, the default value is "\r\n" on Windows® and "\n" on UNIX® and macOS.

Example: LineEnding="\n"

Example: LineEnding="\r\n"

Character encoding scheme associated with the file, specified as "system" or one of the values in the table. The default value is "system", which uses your system default encoding to write the file.

"Big5"

"ISO-8859-1"

"windows-874"

"Big5-HKSCS"

"ISO-8859-2"

"windows-949"

"CP949"

"ISO-8859-3"

"windows-1250"

"EUC-KR"

"ISO-8859-4"

"windows-1251"

"EUC-JP"

"ISO-8859-5"

"windows-1252"

"EUC-TW"

"ISO-8859-6"

"windows-1253"

"GB18030"

"ISO-8859-7"

"windows-1254"

"GB2312"

"ISO-8859-8"

"windows-1255"

"GBK"

"ISO-8859-9"

"windows-1256"

"IBM866"

"ISO-8859-11"

"windows-1257"

"KOI8-R"

"ISO-8859-13"

"windows-1258"

"KOI8-U"

"ISO-8859-15"

"US-ASCII"

 

"Macintosh"

"UTF-8"

 

"Shift_JIS"

 

Example: Encoding="system" uses the system default encoding.

Writing mode, specified as "overwrite" or "append". For an existing file, "overwrite" will overwrite the file and "append" will append data to the file.

Example: WriteMode="append"

How to handle trailing line endings in a file, specified as "auto", "always", or "never".

  • "auto" – End the file with a trailing line ending. If the text does not have one, an extra line is appended.

  • "always" – Always append a line ending to the file.

  • "never" – Never append a line ending to the file.

Example: TrailingLineEndingRule="always"

Version History

Introduced in R2022a