Matlab 文件读写(I/O)和字符串操作常用命令

来源:互联网 发布:孙兴慜数据 编辑:程序博客网 时间:2024/04/28 23:19

Import Text Data Files with Low-Level I/O
基本函数:
fscanf: 读写在text or ASCII file中的数据
fgetl: 一次读一行不保留换行符号
fgets: 一次读一行保留换行符号
fread:读二进制文件,与fwrite 对应
fscanf:


A = fscanf(fileID, format) reads and converts data from a text file into array A in column order. To convert, fscanf uses the format and the encoding scheme associated with the file. To set the encodings cheme, use fopen. The fscanf function reapplies the format throughout the entire file, and positions the file pointer at the end-of-file marker. If fscanf cannot match the format to the data, it reads only the portion that matches into A and stops processing.

A = fscanf(fileID, format, sizeA) reads sizeA elements into A, and positions the file pointerafter the last element read. sizeA canbe an integer, or can have the form [m,n].

sizeA

example:




Reading Data in a Formatted Pattern
To import text files that importdata and textscan cannotread, consider using fscanf。
fscanf: 自主设计格式。


Reading Data Line-by-Line
MATLAB provides two functions that read lines from filesand store them in string vectors: fgetl and fgets.The fgets function copies the newline characterto the output string, but fgetl does not.
exmaple:


Testing for End of File (EOF)



2014.04.04


Add:
dir(): 查询文件夹中的文件,可加后缀查找指定格式的文件。如 dir(['fk\' , '*.jpg'])表示查找fk文件夹下后缀为‘.jpg’的文件
mkdir():创建文件夹。 mkdir('fj') 在当前目录下创建名为fj文件夹;mkdir('fj\fi')表示当前路径下的fj文件夹里创建fi子文件夹。
rmdir( 'fl'): 用于删除文件夹
copyfile( srcpath , despath): 拷贝文件或者文件夹

一、matlab对路径的操作
1. filesep
返回当前平台目录分隔符,window: '\' ; linux : '/'

2. fullfile
将若干字符串连成完整路径
eg. pathFile = fullfile( 'D:' , 'Matlab' , 'love.txt' ) ;
result:  D:\Matbla\love.txt

3. fileparts
将完整文件路径分为四个部分: 路径, 文件名,扩展名,版本号。
eg. [pathstr , name , ext ] = fileparts( 'D:\Matlab\love.txt');
result : 
pathstr = 'D:\Matlab'
name = love
ext = .txt

4. pathsep
返回当前平台路径分割符。windows: ';'(分号) ; Linux: ‘:’冒号

5. exist
判断目录或者文件是否存在。

6. which

可以通过一个函数或脚本名称得到它的完整路径,同时还能处理函数重载的情况,

例如: 

>> which abs(0) 

C:\MATLAB7\toolbox\matlab\elfun\@double\abs.bi % double method 

>> which abs(single(0)) C:\MATLAB7\toolbox\matlab\elfun\@single\abs.bi % single method 

7.isdir

判断路径是否为一个目录

8.dir

列出一个目录的内容,返回值为结构体数组类型。结构体{ name;date ; bytes; isdir}

9.cd

切换当前工作目录

》cd('c:/tooblx/matlab/demos')

>> cd .. %切换到当前工作目录

10. pwd

返回当前工作目录

》pwd

11.path

用于搜索路径


12. addpath 和rmpath

用于对matlab的搜索路径添加和删除

》 addpath( 'directory')

>> rmpath( 'dirctory')

13. what

用于显示某目录下存在的matlab文件;


14. path2rc

保存matlab的搜索路径到pathdef.m中

========================================================================

二、matlab对文件的操作

1. fopen

打开文件或者获得文件信息

2. fclose

3. load

可用于读数值型文件,并且无须打开文件,但要求文件中的内容所有行列数目相同。

4. fread

读取文件中的内容,需要和fopen,fclose配合使用,返回ASCII,可指定返回字节数和返回格式。

fid = fopen( f , 'r')

a= fread(fid) %读取文件全部内容,返回每个字节ASCII

b= fread(fid ,5) %读取文件中前5个ASCII

c=fread(fid,3 ,'uint8=>char')%读取5个字符并以char格式返回

d= fread( fid, '*char')% 读取文件全部内容并以字符串的格式返回

e2 = fread(fid , 8 , '*char'); %读取fid指向位置开始的后边8个字符,并以字符串格式返回。

fclose(fid)

5. fscanf

读取指定格式的内容,可指定读取字节数。

fscanf(fid,format,n) : fid 文件句柄;format:按格式读入内容;n:读入的字节数

6. fgetl

读取文件中一行内容,不包括换行符

tline = fgetl(fid)

7. fgets

读取文件中一行,包括换行符

tline = fgets(fid)

tline = fgets(fid , n)

8. fwrite

将数据按照二进制格式写入文件

fwrite(fid,magic(5),'integer*4') %按照4位整型类型写入一个矩阵到文本

9. fprintf

将数据按照指定格式写入文本中。

fprintf( fid ,'%6.3f'%12.8f\n',y)

10. fseek

对指定文件进行位置设定。

fseek( fid,19,'bof')% ?

11. ftell

得到文件位置指针

pos = ftell(fid)

12. ferror

查询关于文件错误的输入和输出

message = ferror(fid) % 返回错误信息

message = ferror( fid , 'clear') % 返回错误信息并清空

13.feof

判断是否为文件末尾

while ~feof(fid)

=============================================================================

Matlab字符串操作

1. length

获取字符串的长度

length(str)

2. strcat

连接两个字符串,最右边的字符串的空格被删除

strcat(a,c)

3. strvcat

连接多行字符串,每行长度可不等,自动把非最长的字符串最右边补空格。会忽略空字符串。

strvcat( str1 ,str2 , strm)

4.char 

连接字符串,空字符串会被空格填满。与strvcat不同

char(str1, str2 , str3)

5. strcmp , strncmp , strcmpi , strncmpi

strcmp: 比较两个字符串是否相等

strncmp: 比较两个字符串前n个字符是否相等

strcmpi:比较两个字符串是否相等,忽略大小写

strncmpi:比较两个字符串前n个字符串是否相等,忽略大小写

6. isletter , isspace , isstrprop

isletter: 检测字符串是否有大写

isspace:检测是否有空格回车制表符等分割符

isstrprop:检测每个字符是否属于指定的范围

7. strrep 

strrep: 字符串替换,区分大小写

8.strfind , findstr ,strmatch , strtok

strfind: 查找str中是否含有指定的字符串,有返回位置,没有返回空数组

findstr: 查找str1,str2,较短的字符串在较长字符串中出现的位置

strmatch(pattern,str): 检查pattern是否和str最左侧部分一致

strtok(str,char):返回str中由char指定的字符串前的部分和之后的部分(根据指定的字符,拆分字符串)

9. blank(n),delblank,strtim

blanks(n): 创建有n个空格组成的字符串

deblank(str):去掉字符串尾部的空格

strstim: 去除字符串首尾的空格、制表符、回车符

10. lower , upper, sort 

lower: 将字符串中的字母转化为小写

upper:将字符串中的字母转成大写

sort:字符串按照ASCII值排序

11.num2str , str2num , mat2str , int2str

num2str: 将数字转化为数字字符串

str2num:将数字字符串转化为数字

mat2str: 将数组转成字符串

int2str:将数值数组转化为整数数字组成的字符数组

12. strsplit,strjoin

strsplit:  将字符串按照指定的delimiter进行拆分

strjoin: 将strings in a cell array 连接成为单个string



0 0
原创粉丝点击