不推荐使用
textread
。请改用
textscan
。目前没有删除
textread
的计划。
使用
textscan
函数从文本文件或字符串中读取格式化数据。与
textread
函数相比,使用
textscan
的工作流有几项优势。
-
不同于
textread
,由
textscan
提供的输出是元胞数组。
-
文件指针在对
textscan
的调用之间保持不变,从而允许您以分块形式读取数据。
-
textscan
工作流支持从远程位置读取。
-
textscan
生成的错误消息为如何调整语法和工作流提供了清晰的指导。
下表显示了
textread
的一些典型用法,以及如何更新代码以改用
textscan
。
不推荐
|
推荐
|
[date,level,x,y,answer] = textread('scan1.dat',...
'%s %s %f %d %s',1)
|
filename = 'scan1.dat';
fileID = fopen(filename);
C = textscan(fileID,'%s %s %f %d %s');
fclose(fileID);
celldisp(C)
|
data = textread('data.csv','','delimiter',',',...
'emptyvalue',NaN)
|
filename = 'data.csv';
fileID = fopen(filename);
C = textscan(fileID,'%f %f %f %f %u8 %f', ...
'Delimiter',',','EmptyValue',NaN);
fclose(fileID);
celldisp(C)
|