- Output Data
fid=fopen('hello.txt','w'); %需要改文件名称的地方
fprintf(fid,' %10.3f \n',data); %data:需要导出的变量名称,10位有效数字,保留3位小数(包含小数点),f为双精度,g为科学计数法
fclose(fid);
When the data is two rows, after the data is read, the format in the document is two columns, which can be directly written as:
fid=fopen('hello.txt','w'); %需要改文件名称的地方
fprintf(fid,' %10.3f %10.3f \n',data); %data:需要导出的变量名称,10位有效数字,保留3位小数(包含小数点),f为双精度,g为科学计数法
fclose(fid);
Note that when the data is in two columns, you need to convert the data into two rows before outputting:
fid=fopen('hello.txt','w'); %需要改文件名称的地方;
fprintf(fid,' %10.3f %10.3f \n',data'); %data注意转置;
fclose(fid);
- read data into variable
fid=fopen('hello.txt','r'); %需要改文件名称的地方;
size_position=[3 Inf]; %假设数据为3列。Inf指每列的所有数据;
[A,count]=fscanf(fid,'%f%f%f',size_position); %data:需要导出的数据名称,10位有效数字,保留3位小数(包含小数点),f为双精度,g为科学计数法;
%A为储存数据的变量名;
%count为被读取的数据长度;
%也可以直接输出给变量:A=fscanf(fid,'%10.3f');
fclose(fid); %注意一定要关闭文件,否则会影响文件的状态!
A=A'; %文件中为列的数据,读入到变量之后为行,如果需要变量呈现列,需要对矩阵单独转置;