readlines
说明
S = readlines(
基于文件创建一个字符串数组,并通过一个或多个名称-值对组参数指定其他选项。例如,filename
,Name,Value
)'EmptyLineRule','skip'
跳过空行。
示例
基于文本文件创建字符串数组
通过将文本文件中的每行作为一个单独字符串读取,创建一个 4×1 字符串数组。
lines = readlines("badpoem.txt")
lines = 4x1 string
"Oranges and lemons,"
"Pineapples and tea."
"Orangutans and monkeys,"
"Dragonflys or fleas."
忽略文本文件中的空行
查看文件 capital_cities.txt
的内容。
type 'capital_cities.txt'
Boston, Massachusetts Albany, New York Sacramento, California Columbus, Ohio Santa Fe, New Mexico
通过在读取 capital_cities.txt
时跳过空行来创建一个 5×1 字符串数组。该数组仍然包含输入中的空白字符,但不包含空行。
lines = readlines("capital_cities.txt","EmptyLineRule","skip")
lines = 5x1 string
" Boston, Massachusetts"
"Albany, New York"
" Sacramento, California"
"Columbus, Ohio"
" Santa Fe, New Mexico"
删除文本周围的空白
查看文件 capital_cities.txt
的内容。
type 'capital_cities.txt'
Boston, Massachusetts Albany, New York Sacramento, California Columbus, Ohio Santa Fe, New Mexico
将 'WhitespaceRule'
的值指定为 'trimleading'
,以删除每行文本之前的空白。数组不再包含输入中的前导空白字符,但仍保留空行。
lines = readlines("capital_cities.txt","WhitespaceRule","trimleading")
lines = 10x1 string
"Boston, Massachusetts"
""
"Albany, New York"
""
"Sacramento, California"
""
"Columbus, Ohio"
""
"Santa Fe, New Mexico"
""
输入参数
filename
— 要读取的文件的名称
字符向量 | 字符串标量
要读取的文件的名称,指定为字符向量或字符串标量。
根据文件的位置,filename
可以采用下列形式之一。
位置 | 形式 | ||||||||
---|---|---|---|---|---|---|---|---|---|
当前文件夹或 MATLAB® 路径上的文件夹 | 指定 示例: | ||||||||
文件夹中的文件 | 如果该文件不在当前文件夹或 MATLAB 路径下的文件夹中,则在 示例: 示例: | ||||||||
Internet URL | 如果文件指定为 Internet 统一资源定位器 (URL),则 示例: | ||||||||
远程位置 | 如果文件存储在远程位置,则
根据远程位置,
有关详细信息,请参阅处理远程数据。 示例: |
数据类型: char
| string
名称-值参数
将可选的参数对组指定为 Name1=Value1,...,NameN=ValueN
,其中 Name
是参数名称,Value
是对应的值。名称-值参数必须出现在其他参数之后,但参数对组的顺序无关紧要。
在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name
引起来。
示例: 'EmptyLineRule','skip'
指定在导入文件时忽略空行。
LineEnding
— 行尾字符
{'\n','\r','\r\n'}
(默认) | 字符向量 | 字符串标量 | 字符向量元胞数组 | 字符串数组
行尾字符,指定为由 'LineEnding'
和一个字符向量或字符串组成的逗号分隔对组。该字符向量必须为 '\r\n'
,或须指定单个字符。常用的行尾字符是换行符 ('\n'
) 或回车符 ('\r'
)。如果您指定 '\r\n'
,则导入函数会将 \r
或 \n
以及两者的组合 (\r\n
) 视为行尾字符。
默认的行尾序列是 \n
、\r
或 \r\n
,具体取决于您的文件的内容。
示例: 'LineEnding','\n'
示例: 'LineEnding','\r\n'
数据类型: char
| string
| cell
Whitespace
— 要视为空白的字符
{' ','\b','\t'}
| 字符向量 | 字符串标量
要视为空白的字符,指定为包含一个或多个字符的字符向量或字符串标量。
示例: 'Whitespace',' _'
示例: 'Whitespace','?!.,'
WhitespaceRule
— 处理空白的过程
'preserve'
(默认) | 'trim'
| 'trimleading'
| 'trimtrailing'
处理数据中行周围空白的过程,指定为 'preserve'
、'trim'
、'trimleading'
或 'trimtrailing'
。
空白规则 | 行为 |
---|---|
'preserve' | 保留前导和尾部空白。 |
'trim' | 删除前导和尾部空白。 |
'trimleading' | 仅删除前导空白。 |
'trimtrailing' | 仅删除尾部空白。 |
示例: 'WhitespaceRule','trim'
数据类型: char
| string
EmptyLineRule
— 处理空行的方式
'read'
(默认) | 'skip'
| 'error'
处理数据中的空行的方式,指定为 'skip'
、'read'
或 'error'
。
空行规则 | 行为 |
---|---|
'read' | 导入空行。 |
'skip' | 跳过空行。 |
'error' | 显示错误消息并中止导入操作。 |
示例: 'EmptyLineRule','skip'
数据类型: char
| string
Encoding
— 字符编码方案
''
(默认) | 'UTF-8'
| 'system'
| 'ISO-8859-1'
| 'windows-1251'
| 'windows-1252'
| ...
与文件关联的字符编码方案,指定为以逗号分隔的对组,其中包含 'Encoding'
和 'system'
或标准字符编码方案名称。'Encoding','system'
使用系统默认编码。
如果您没有指定任何编码,则该函数在读取文件时将使用自动字符集检测来确定编码。
数据类型: char
| string
WebOptions
— HTTP
或 HTTPS
请求选项
weboptions
对象
HTTP
或 HTTPS
请求选项,指定为 weboptions
对象。当指定的 filename
是包含协议类型 "http://"
或 "https://"
的 Internet URL 时,由 weboptions
对象确定如何导入数据。
版本历史记录
在 R2020b 中推出
另请参阅
writelines
| readtable
| readtimetable
| readmatrix
| readvars
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
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)