当前位置:网站首页>MATLAB file operations
MATLAB file operations
2022-08-02 16:34:00 【智赵】
目录
一、文件打开与关闭
1、fopen函数
用于打开文件,其调用格式为:
fid = fopen(filename,permission);
其中, fidFor the file identification number,Open the file success,fidReturns an integer,Used to identify the file;Opened the files is not successful,fid值为-1.
filename:待操作的文件名;
permission:Allows the use of documents in much the same way.
文件的使用方式如下:
• ‘r’: To read the way to open a file;
• ‘w’: To write the way to open a file;
• ‘a’: At the end of the file to add data;
• ‘r+’: To open a file to read and write way;
• ‘w+’:Open or create file used to read and write,Cover the content of the existing;
• ‘a+’: Read and write files in the form of additional.
2、fclose函数
用于关闭已打开的文件,其调用格式为:
status = fclose(fid);
fidIs to close the file id.如果fid为all,Then close all open files,But except for standard file,即屏幕、键盘.返回 0 表示关闭成功,返回-1Said closing is not successful.
注意:fopen与fcloseTo appear at the same time in a program,即打开一个文件,After the operation to close the file.
二、文本文件的读写
1、fscanf函数和fprintf函数
fscanfFunction is used to read the contents of a text file,fprintfFunction is used for writing data to a text file.
调用格式为:
[A, count] = fscanf(fid, fmt, size);
count = fprintf(fid, fmt, A);
其中,AUsed to store data read and write,countReturns the number of data element in the success, speaking, reading and writing;参数fidFor the file identification number,fmtUsed to control the read data format,size用于指定A的大小.
fmt由%And each of which format,Common numerical format control characters are as follows:
• ‘%d’: 整数;
• ‘%f’:The decimal form of number;
• ‘%e’:Scientific notation forms of real number;
• ‘%c’:字符;
• ‘%s’: 字符串;
在%Can also add data width after.
size可取值:
• n: 指定读取n个数据;
• Inf:Specifies read all the data in the file;
• [m,n]:指定读取m× n个数据,Data stored in the column order to matrixA.
2、案例1
例1 文件“Observation records.txt”的内容如图所示,读取文件前10行的数据.
fid=fopen('Observation records.txt','r');
title=fscanf(fid,'%s',6);
qxsj=[];
for i=1:10
qxsj{
i,1}=fscanf(fid,'%s',1);
qxsj{
i,2}=fscanf(fid,'%s',1);
qxsj{
i,3}=fscanf(fid,'%f',1);
qxsj{
i,4}=fscanf(fid,'%f',1);
qxsj{
i,5}=fscanf(fid,'%f',1);
qxsj{
i,6}=fscanf(fid,'%s',1);
end
fclose(fid);
三、二进制文件的读写
1、fread函数和fwrite函数
freadFunction is used to read binary file,fwriteFunction to write data into the file.
调用格式为:
[A, count] = fread(fid, size, precision, skip);
count = fwrite(fid,A,precision);
其中,AUsed to store data read and write,countReturns the number to read and write data to the success of.fidFor the file identification number,precisionSpecify the type of reading and writing data,size用于指定A的大小,skipSpecify the periodically in proportion to skip some data.
2、案例2
例2 计算y=exp(x)sinx,其中x∈[0,2π].将x、y写入二进制文件“模拟数据.dat”.
fid=fopen('模拟数据.dat','w');
x=linspace(0,2*pi,100);
y=exp(x).*sin(x);
count=fwrite(fid, [x; y], 'double');
fclose(fid);
四、The data file location
1、fseek函数
This function is used to change the file pointer position,其调用格式为:
fseek(fid, offset, origin);
其中参数fidFor the file identification number,offsetNumber of bytes to said location pointer relative mobile;originSaid location pointer reference.
origin的可取值:
• 'cof '或0:表示文件指针的当前位置;
• 'bof '或-1:表示文件的开始位置;
• 'eof’或1:表示文件的结束位置.
2、ftell函数
Used to query the file pointer to the current location,其调用格式为:
position = ftell(fid);
The return value from the header to the number of bytes of pointer to the current position.若返回值为-1,According to get the current file position failed.
3、feof函数
Used to judge whether the current file location pointer reaches the end of,其调用格式为:
status = feof(fid);
When to end of file position,返回值为1,否则为0.
4、案例3
例3 Read the case2In the generated file after40组数据,并绘制图形.
fid=fopen('模拟数据.dat','r');
status=fseek(fid, -40*2*8, 'eof');
x=[]; y=[];
while ~feof(fid)
x=[x; fread(fid,1,'double')];
y=[y; fread(fid,1,'double')];
end
plot(x,y);
fclose(fid);
参考资料
边栏推荐
猜你喜欢
随机推荐
Mysql-Explain与索引详解
详解C语言中的位操作运算符可以怎么用?
golang中使用泛型
【软件测试】进阶篇
appium 报错:AttributeError:
Cmd Markdown Formula Guidebook
makefile——pattern rule
小知识系列:Fork之后如何与原仓库分支同步
小知识点系列-基于H2数据库单元测试
【软件测试】测试基础讲解
Scala的模式匹配与样例类
DOM —— 事件对象
解决跨域问题的方法 --- JSONP
【故障诊断】基于PSO_VMD_MCKD方法的风机轴承微弱故障诊断
DOM —— 事件类型
解决跨域问题的方法 --- CORS
数据库性能优化的误区!
MATLAB文件操作
【软件测试】项目中关于测试人员的简单介绍
怎么使用 smart_rtmpd 的纯 webrtc 功能?