writelines
Description
writelines(
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.lines
,filename
,Name=Value
)
Examples
Write Text to Local File
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 Text to Existing File
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
lines
— Text to write
string array | character vector | cell array of character vectors
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"]
filename
— File name
string scalar | character vector
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
Example:
| ||||||||
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
Example:
Example:
| ||||||||
Internet URL | If the file is specified as an internet uniform resource locator
(URL), then Example:
| ||||||||
Remote Location | If the file is stored at a remote location, then
Based on the remote location,
For more information, see Work with Remote Data. Example:
|
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.
LineEnding
— End-of-line characters
string scalar | character vector
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"
Encoding
— Character encoding scheme
"system"
(default) | "UTF-8"
| "ISO-8859-1"
| "windows-1251"
| "windows-1252"
| ...
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
Example: Encoding="system"
uses the system default
encoding.
WriteMode
— Writing mode
"overwrite"
(default) | "append"
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"
TrailingLineEndingRule
— How to handle trailing line endings
"auto"
(default) | "always"
| "never"
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
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)