matlab清除数据格式化,Matlab实例学习-----------格式化文本 读 操作

(1)使用fscanf读取文本

语法:

A = fscanf(fileID, format)

A = fscanf(fileID, format, sizeA)

[A, count] = fscanf(...)

参数介绍:

matlab清除数据格式化,Matlab实例学习-----------格式化文本 读 操作_第1张图片

matlab清除数据格式化,Matlab实例学习-----------格式化文本 读 操作_第2张图片

matlab清除数据格式化,Matlab实例学习-----------格式化文本 读 操作_第3张图片

实例:

% 1、打开文件

fid=fopen('读取文件.txt','r');

% 2、读取文件

a=fscanf(fid,'%d');

%读取结果为n行1列的形式,n为文本文件中数字的个数

% 3、关闭文件

fclose(fid);其中,读取文件.txt 中的内容是:

1 2 3 4 5

2 3 4 5 6

3 4 5 6 7

4 5 6 7 8

5 6 7 8 9

6 7 8 9 0(2)textscan读取格式化文本

语法:

C = textscan(fileID,formatSpec)

C = textscan(fileID,formatSpec,N)

C = textscan(str,formatSpec)

C = textscan(str,formatSpec,N)

C = textscan(___,Name,Value)

[C,position]= textscan(___)

formatSpec:

matlab清除数据格式化,Matlab实例学习-----------格式化文本 读 操作_第4张图片

matlab清除数据格式化,Matlab实例学习-----------格式化文本 读 操作_第5张图片

实例:

% 使用textscan读取格式化文本文件

fid=fopen('读取文件.txt','r');

a=textscan(fid,'%d %d %d %d %d ');

fclose(fid);

% 结果

% a{1} %用来访问具体的某一列

% ans =

%

% 1

% 2

% 3

% 4

% 5

% 6

(3)使用HeaderLines忽略文本的标题

% 忽略文本标题

fid=fopen('读取文件.txt','r');

a=textscan(fid,'%d %d %d %d %d ','HeaderLines',2);%忽略两行标题

fclose(fid);结果:从第三行开始读取文本内容

你可能感兴趣的:(matlab清除数据格式化)