这是我做过的程序,里面涉及文件读取,字符串的写入(一大技巧)
程序简介:
1、功能:根据已知数学模型,利用可测量的三点数据和三点到质心的距离计算质心; 结果保留在“质心数据.txt”中。
2、前提条件: 要求数据文件的采样率要保持稳定; 而且时间一致 。
3、3个点的数据格式 如:2008年08月04日 09:20:55.700 **.891702 ***.547075 64.6729 -***787.8792 ***283.3667 ***3007.1063 0.0528 0.0958 0.0085 0.1098 8
4、作者rsj
%}
close all;
clc;
% 测量数据的地址\文件名
Path='c:\**\';
Infile1='0401.txt'; Infile2='0402.txt'; Infile3='0403.txt'; Outfile1='Outfile1.txt';
%Outfile='zhixin.txt';
%判断三个文件是否能够打开
[fid1,message]= fopen([Path,Infile1],'r');
if fid1== -1
disp(sprintf( 'Unable to open the file ''%s''!',Infile1))
else
disp(sprintf( 'The file ''%s'' successfully opened!',Infile1))
end
[fid2,message]= fopen([Path,Infile2],'r');
if fid2 == -1
disp(sprintf( 'Unable to open the file ''%s''!',Infile2))
else
disp(sprintf( 'The file ''%s'' successfully opened!',Infile2))
end
[fid3,message]= fopen([Path,Infile3],'r');
if fid3 == -1
disp(sprintf( 'Unable to open the file ''%s''!',Infile3))
else
disp(sprintf( 'The file ''%s'' successfully opened!',Infile3))
end
%读取数据
[date,t1,b1,l1,h1,x1,y1,z1,vx1,vy1,vz1,temp,temp]=textread([Path,Infile1], '%s %s %f%f%f %f%f%f %f%f%f %f %d');
[date,t2,b2,l2,h2,x2,y2,z2,vx2,vy2,vz2,temp,temp]=textread([Path,Infile2], '%s %s %f%f%f %f%f%f %f%f%f %f %d');
[date,t3,b3,l3,h3,x3,y3,z3,vx3,vy3,vz3,temp,temp]=textread([Path,Infile3], '%s %s %f%f%f %f%f%f %f%f%f %f %d');
fid=fopen([Path,Outfile1],'w');
fidph=fopen('c:\**\zhixin.txt','w');
% xyz=[-****787.938 ****340.800 ****942.599 ];
row1=length(t1);
row2=length(t2);
row3=length(t3);
data1=[x1,y1,z1];
data2=[x2,y2,z2];
data3=[x3,y3,z3];
for i=1:row1
for j=1:row2
if strcmp(t1(i),t2(j))==1
t1(i); t2(j);
for k=1:row3
if strcmp(t1(i),t3(k))==1&strcmp(t2(j),t3(k))==1
t1(i),data1(i,:),t2(j),data2(j,:),t3(k),data3(k,:)
fprintf(fid,\r\n',t1(i,1));
% fprintf(fid,' '%12S %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f \r\n',t{i},data1(i,:),data2(j,:),data3(k,:));
break
end
end
end
end
end
fclose('all');
1