sscanf
从字符串中读取格式的数据
句法
a =sscanf(str,formatSpec)
A = SSCANF(Str,FormatsPec,Sizea)
[[一个,,,,n] = sscanf(___)
[[一个,,,,n,,,,errmsg] = sscanf(___)
[[一个,,,,n,,,,errmsg,nextindex] = sscanf(___)
描述
从中读取数据一个
= sscanf(str
,,,,formatSpec
)str
,,,,converts it according to the format specified byformatSpec
, 和returns the results in an array.str
字符数组或吗字符串标量。Thesscanf
function repeatedly appliesformatSpec
到字符中的序列str
until it either reaches the end ofstr
或不匹配formatSpec
一系列字符。如果str
是一个超过一行的角色数组,sscanf
按列顺序读取字符。
将输出数组的大小设置为一个
= sscanf(str
,,,,formatSpec
,,,,sizeA
)sizeA
and then reads data fromstr
into the output array.sizeA
must be a positive integer or have the form[m n]
,,,,wherem
andn
是积极的整数。
例子
将字符向量转换为数字
Create a character vector that represents several numbers separated by whitespace characters. Convert the character vector to a column vector of numbers.sscanf
将空格字符视为数字之间的分离器。
chr ='2.7183 3.1416 0.0073'
chr ='2.7183 3.1416 0.0073'
a = sscanf(chr,'%f')
a =2。7183 3.1416 0.0073
Convert Text and Resize Output Array
创建一个表示几个数字的字符串,并使用sscanf
。指定输出数组的大小。
从R2017A开始,您可以使用双引号来创建字符串string
功能。
str =“ 2.7183 3.1416 0.0073”
str =“ 2.7183 3.1416 0.0073”
a =sscanf(str,'%f',[1 3])
a =2。7183 3.1416 0.0073
Convertstr
到2 x-2矩阵。因为str
仅代表三个数字,sscanf
pads一个
用足够的零填充矩阵。
a =sscanf(str,'%f',[2 2])
a =2.7183 0.0073 3.1416 0
文本中发现的计数元素
Create a string that contains numbers separated by whitespace characters. Count the elements thatsscanf
puts into the output array when it converts the string to numbers.
从R2017A开始,您可以使用双引号来创建字符串string
功能。
str ="78 72 64 66 49"
str =“ 78 72 64 66 49”
计算输出数组中的元素一个
。使用字符串中的数字使用%d
operator.%d
matches integers separated by whitespace. To return the number of elements in一个
,指定第二个输出参数。
[[一个,,,,n] = sscanf(str,'%d')
a =78 72 64 66 49
n = 5
Display Error Message
创建一个字符串并从中读取数据。什么时候sscanf
fails to convert all of the input string, display the error message.
从R2017A开始,您可以使用双引号来创建字符串string
功能。
str =“ 3.14159是PI的前6位数字”
str =“ 3.14159是PI的前6位数字”
转换数字str
。自从str
还包含字符%F
无法匹配,sscanf
返回错误消息。sscanf
stops processing as soon as it encounters the word'are'
因为它不能转换为数字。
[a,n,errmsg] = sscanf(str,'%f')
a = 3.1416
n = 1
errmsg= 'Matching failure in format.'
Return Last Position Scanned
Create a character vector and read data from it. Whensscanf
fails to convert all of the input, return the index that immediately follows the position at whichsscanf
停了下来。使用此索引显示未频率的输入。
chr ='3.14159是PI的前6位数字'
chr ='3.14159是PI的前6位数字'
转换数据chr
。返回索引。
[a,〜,〜,NextIndex] = sscanf(chr,'%f')
a = 3.1416
NextIndex= 9
Display the characters fromchr
那sscanf
没有扫描。
chr(nextindex:end)
Ans ='是PI'的前6位数字
匹配指定的字符
Create a string that contains several temperatures, indicated by the degree symbol and the letterF
。将温度转换为数字阵列。
从R2017A开始,您可以使用双引号来创建字符串string
功能。To insert the degree symbol (char(176)
),,,,use the插入
功能。
t ="78F 72F 64F 66F 49F";degreesymbol = char(176);t = insertbefore(t,'F',degreesymbol)
t ="78°F 72°F 64°F 66°F 49°F"
Return the temperatures as a numeric array.
a = sscanf(t,strcat(“%d”,devreesymbol,“F”))
a =78 72 64 66 49
输入参数
str
- 输入文本进行扫描
字符阵列|字符串标量
输入text to scan, specified as a character array or string scalar. Ifstr
是一个字符数组,然后它可以具有多个行,并且sscanf
按列顺序读取字符。
Data Types:char
|string
formatSpec
- 输入字段的格式
formatting operators
Format of the input fields, specified using formatting operators.formatSpec
可以是单引号中的字符向量,也可以是字符串标量。
数字字段
该表列出了可用的转换说明符,以将文本转换为数字输出。sscanf
将值转换为其十进制(基本10)表示。
数字字段类型 |
Conversion Specifier |
描述 |
---|---|---|
Integer, signed |
|
基础10。 |
|
值确定基础:
|
|
|
64位值,基础10、8或16。 |
|
Integer, unsigned |
|
基础10。 |
|
基地8(八分之一)。 |
|
|
基地16(十六进制)。 |
|
|
64位值,基础10、8或16。 |
|
浮点数 |
|
浮点值。输入字段可以包含以下任何一个(不敏感): |
Character Fields
This table lists available conversion specifiers to convert text so that the output is a character array.
字符字段类型 |
Conversion Specifier |
描述 |
---|---|---|
Character vector or string scalar |
|
阅读文字直到 |
|
Read any single character, including whitespace. |
|
模式匹配 |
|
在括号中仅读取字符,直到第一个不匹配的字符或空格。 例子: |
如果formatSpec
包含数字和字符指定的组合,然后sscanf
将每个字符转换为其数字等效。
Optional Operators
字段和字符要忽略
sscanf
阅读all numeric values and characters in sequence, unless you tell it to ignore a particular field or a portion of a field. To skip fields, insert an asterisk (*
)符号百分比之后(%GydF4y2Ba
)。例如,要跳过整数,请指定%*d
。Field Width
To specify the maximum number of digits or text characters to read at a time, insert a number after the percent character. For example,
%10C
一次最多可读取10个字符,包括Whitespace。%4F
一次读取多达四位数,包括小数点。Literal Text to Ignore
sscanf
在转换说明符之前或之后忽略指定的文本。例子:
级别u
阅读'1级'
作为1
。例子:
%uStep
阅读'2step'
作为2
。
Data Types:char
|string
sizeA
— Dimensions of output array
inf
(default) | integer | two-element row vector
输出阵列的尺寸,一个
,,,,specified asinf
,,,,a positive integer, or a two-element row vector.
形式 |
描述 |
---|---|
|
Read input to the end. |
|
最多阅读 |
|
最多阅读 |
Data Types:double
|single
|int8
|INT16
|INT32
|int64
|uint8
|UINT16
|uint32
|uint64
Output Arguments
一个
- 输出数据
column vector | matrix | character array
输出数据,作为列向量,矩阵或字符数组返回。班级和大小一个
depend on the conversions specified byformatSpec
以及由sizeA
:
如果
formatSpec
仅包含numeric specifiers, then一个
是数字列向量。如果您还指定sizeA
争论,然后一个
是指定尺寸的矩阵,如有必要,用零填充。如果输入包含的少于sizeA
values, then the size of一个
是smaller thansizeA
。相反,它是存储从输入扫描的值所需的大小。如果
formatSpec
仅包含64位签名的整数指定符,然后一个
是of classint64
。如果
formatSpec
仅包含64-bit unsigned integer specifiers, then一个
是of classuint64
。Otherwise,
一个
是of classdouble
。
如果
formatSpec
仅包含%C
or%s
然后一个
是角色向量。如果you also specifysizeA
, 然后一个
是a character array, and is padded as necessary with null characters. (The null character is a control character with the value zero.) If the input contains fewer thansizeA
字符,然后大小一个
是smaller thansizeA
。相反,它是存储从输入扫描的字符所需的大小。如果
formatSpec
包含数字和字符指定的组合,然后一个
是数字,类double
, 和sscanf
将每个字符转换为其数字等效。This conversion occurs even whenformatSpec
explicitly skips all numeric fields (for example,formatSpec
是'%*d %s'
)。如果
sscanf
无法匹配所有输入formatSpec
, 然后一个
可numeric or a character array. The class of一个
depends on the values thatsscanf
在停止处理之前读取。
Data Types:double
|int64
|uint64
|char
n
— Number of elements read into output array
整数
读取输出数组的元素数量,返回为整数。
Data Types:double
errmsg
— Error message
character vector
错误消息,返回为字符向量。如果str
包含任何数据sscanf
cannot convert, thenerrmsg
包含一个错误消息。如果sscanf
成功转换所有数据,然后errmsg
是an empty character vector.
Data Types:char
NextIndex
— Position after last character scanned
整数
最后一个角色扫描后的位置,以整数返回。
Data Types:double
提示
Format specifiers for the reading functions
sscanf
andfscanf
differ from the formats for the writing functionsSprintf
andfprintf
。阅读功能不支持精度字段。金宝app宽度字段指定写作的最低限度,但最大的阅读。
Introduced before R2006a
matlab命令
您单击了与此MATLAB命令相对应的链接:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择一个网站
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:。
您还可以从以下列表中选择一个网站:
How to Get Best Site Performance
选择中国网站(中文或英语)以获得最佳场地性能。其他Mathworks乡村网站未针对您所在的访问进行优化。
美洲
- AméricaLatina(Español)
- 加拿大(English)
- 美国(English)