Matlab中textscan函数⽤法
⽬录
textscan函数从⽂本⽂件或字符串读取格式化数据。
语法
C = textscan(fileID,formatSpec)
C = textscan(fileID,formatSpec,N)
C = textscan(chr,formatSpec)
C = textscan(chr,formatSpec,N)
C = textscan(___,Name,Value)
[C,position] = textscan(___)
说明
C = textscan(fileID,formatSpec) 将已打开的⽂本⽂件中的数据读取到元胞数组 C。该⽂本⽂件由⽂件标识符fileID指⽰。使
⽤ fopen 可打开⽂件并获取fileID值。完成⽂件读取后,请调⽤fclose(fileID) 来关闭⽂件。
textscan 尝试将⽂件中的数据与formatSpec中的转换设定符匹配。textscan函数在整个⽂件中按formatSpec重复扫描数据,直⾄formatSpec不到匹配的数据时才停⽌。
C = textscan(fileID,formatSpec,N) 按 formatSpec 读取⽂件数据 N 次,其中 N 是⼀个正整数。要在N个周期后从⽂件读取其他数
据,请使⽤原 fileID 再次调⽤textscan进⾏扫描。如果通过调⽤具有相同⽂件标识符 (fileID) 的textscan恢复⽂件的⽂本扫描,则 textscan 将在上次终⽌读取的点处⾃动恢复读取。
C = textscan(chr,formatSpec) 将字符向量 chr 中的⽂本读取到元胞数组 C 中。从字符向量读取⽂本时,对 textscan 的每⼀次重
复调⽤都会从开头位置重新开始扫描。要从上次位置重新开始扫描,需要指定 position 输出参数。
textscan 尝试将字符向量 chr 中的数据与 formatSpec 中指定的格式匹配。
C = textscan(chr,formatSpec,N) 按 formatSpec N 次,其中 N 是⼀个正整数。
C = textscan(___,NameValue) 使⽤⼀个或多个 Name,Value 对组参数以及上述语法中的任何输⼊参数来指定选项。
[C,position] = textscan(___) 在扫描结束时返回⽂件或字符向量中的位置作为第⼆个输出参数。对于⽂件,该值等同于调
⽤ textscan 后再运⾏ ftell(fileID) 所返回的值。对于字符向量,position指⽰ textscan 读取了多少个字符。
⽰例
1.读取浮点数
celldisp(C)
C{1} =
0.4100
8.2400
3.5700
6.2400
9.2700
读取相同字符向量,将每个值截短⾄⼀位⼩数。
C = textscan(chr,'%3.1f %*1d');
设定符 %3.1f 指⽰字段宽度为 3 位数,精度为 1。textscan 函数读取全部 3 位数,包括⼩数点和⼩数点后的 1 位数。设定符 %*1d 指⽰ textscan 跳过其余位数。
显⽰元胞数组 C 的内容。
2.读取不同类型的数据
加载数据⽂件,并读取具有适当类型的每⼀列。
加载⽂件 scan1.dat 并在⽂本编辑器中预览其内容。屏幕截图如下所⽰。
filename = fullfile(matlabroot,'examples','matlab','scan1.dat');
打开⽂件,⽤正确的转换设定符读取每⼀列。textscan 返回⼀个 1-by-9 元胞数组 C。
fileID = fopen(filename);
C = textscan(fileID,'%s %s %f32 %d8 %u %f %f %s %f');
fclose(fileID);
whos C
Name      Size            Bytes  Class    Attributes
C        1x9              2249  cell
查看C中的每个元胞的 MATLAB® 数据类型。
C
C=1×9 cell
Columns 1 through 5
{3x1 cell}    {3x1 cell}    {3x1 single}    {3x1 int8}    {3x1 uint32}
Columns 6 through 9
{3x1 double}    {3x1 double}    {3x1 cell}    {3x1 double}
检查各个条⽬。请注意,C{1} 和 C{2} 为元胞数组。C{5} 的数据类型为 uint32,因此 C{5} 的前两个元素为 32 位⽆符号整数的最⼤值或 intmax('uint32')。
celldisp(C)
C{1}{1} =
09/12/2005
C{1}{2} =
10/12/2005
C{1}{3} =
11/12/2005
C{2}{1} =
Level1
C{2}{2} =
Level2
C{2}{3} =
Level3
C{3} =
12.3400
23.5400
34.9000
C{4} =
45
60
12
C{5} =
4294967295
4294967295
200000
C{6} =
Inf
-Inf
10
C{7} =
NaN
0.0010
100.0000
C{8}{1} =
Yes
C{8}{2} =
No
C{8}{3} =
No
C{9} =
5.1000 + 3.0000i
2.2000 - 0.5000i
3.1000 + 0.1000i
3.删除字⾯⽂本
从前⼀⽰例的第⼆列数据的每个字段中删除字⾯⽂本 'Level'。下⾯显⽰⽂件的预览。
打开⽂件,并匹配 formatSpec 输⼊中的字⾯⽂本。
filename = fullfile(matlabroot,'examples','matlab','scan1.dat');
fileID = fopen(filename);
C = textscan(fileID,'%s Level%d %f32 %d8 %u %f %f %s %f');
fclose(fileID);
C{2}
ans = 3x1 int32 column vector
1
2
3
查看 C 中的第⼆个元胞的 MATLAB® 数据类型。1-by-9 元胞数组 C 的第⼆个元胞的数据类型现在为 int32。
disp( class(C{2}) )
int32
text函数什么意思
4.跳过每⾏的其余部分
将前⼀⽰例中⽂件的第⼀列读取到元胞数组中,跳过⾏的其余部分。
filename = fullfile(matlabroot,'examples','matlab','scan1.dat');
fileID = fopen(filename);
dates = textscan(fileID,'%s %*[^\n]');
fclose(fileID);
dates{1}
ans = 3x1 cell array
{'09/12/2005'}
{'10/12/2005'}
{'11/12/2005'}
textscan 返回⼀个⽇期元胞数组。
5.指定分隔符和空值转换
加载⽂件 data.csv 并在⽂本编辑器中预览其内容。屏幕截图如下所⽰。请注意该⽂件包含逗号分隔的数据以及空值。
读取该⽂件,将空元胞转换为 -Inf。
filename = fullfile(matlabroot,'examples','matlab','data.csv');
fileID = fopen(filename);
C = textscan(fileID,'%f %f %f %f %u8 %f',...
'Delimiter',',','EmptyValue',-Inf);
fclose(fileID);
column4 = C{4}, column5 = C{5}
column4 = 2×1
4
-Inf
column5 = 2x1 uint8 column vector
11
textscan 返回 1-by-6 元胞数组 C。textscan 函数将 C{4} 中的空值转换为 -Inf,其中 C{4} 与浮点格式关联。因为 MATLAB® 将⽆符号整数-Inf表⽰为 0,所以textscan将 C{5} 中的空值转换为 0 ⽽不是 -Inf。
6.指定要视为空或注释的⽂本
加载⽂件 data2.csv并在⽂本编辑器中预览其内容。屏幕截图如下所⽰。请注意,该⽂件包含可以解释
为注释和其他项
(如 'NA' 或 'na')的数据,这些数据可能表⽰空字段。
filename = fullfile(matlabroot,'examples','matlab','data2.csv');
指定textscan应视为注释或空值的输⼊,并将该数据扫描到 C 中。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。