当前位置:网站首页>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);
参考资料
边栏推荐
猜你喜欢
随机推荐
网络运维系列:GoDaddy Shell DDNS配置
makefile——library
时频分析之Wigner-Ville分布
解决启动filebeat时遇到Exiting: error unpacking config data: more than one namespace configured accessing错误
test3
ADB常用命令--测试人员必备
网络请求——跨域 的概念
WEB自动化之键盘、鼠标操作
FIR滤波器设计之窗函数法
Scala的安装和IDEA的使用(初入茅庐)
GC垃圾回收ZGC
数据库性能优化的误区!
ssm整合
对象头和synchronized的升级
Mysql索引优化二
一线大厂研发流程(转载自鱼皮)
IIR滤波器设计之冲激响应不变法与双线性变换法
移动端UI自动化相关环境配置
DOM —— 事件绑定与解绑
MYSQL5.7详细安装步骤