码迷,mamicode.com
首页 > 其他好文 > 详细

Matlab实例学习-----------格式化文本 读 操作

时间:2014-09-04 22:22:10      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:格式化   matlab   文本      

(1)使用fscanf读取文本

语法:

A = fscanf(fileID, format)

A = fscanf(fileID, format, sizeA)

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

参数介绍:

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

实例:

% 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:

bubuko.com,布布扣

bubuko.com,布布扣

实例:

% 使用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实例学习-----------格式化文本 读 操作

标签:格式化   matlab   文本      

原文地址:http://blog.csdn.net/z1137730824/article/details/39059207

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!