Heidiadalheid 发表于 2015-10-20 08:39

matlab读取fluent数据

matlab画图能力很强,而且其可编程性给处理问题带来很大方便,我尝试将fluent导出的ASCII数据导入matlab处理,原代码如下:
%打开文件并读入数据
fid=fopen('post','r');
name2=input('input the file name\n','s')
fgetl(fid)
data1=fscanf(fid,'%f %f %f %f %f',);
fclose(fid);
fid=fopen(name2,'r');
fgetl(fid);
data2=fscanf(fid,'%f %f %f %f %f',);
fclose(fid);
%对数据按坐标升序排列
xudata1=sortrows(data1',2);
xudata2=sortrows(data2',2);
xudata1=xudata1';xudata2=xudata2';
%找出sym和avg数据
maxr=max(xudata1(3,:));stp=0.0002;%y坐标最大值,求avg时候的x步长
avg1=zeros(2,round(1+0.02/stp));avg1(1,:)=0:stp:0.02;avg2=avg1;%对avg坐标赋初值
j=1;j2=1;jj=1;%j记录avg的当前index;j2记录avg(j)中第一个加入的xudata的index;jj记录sym的当前index
for i=1:length(xudata1)
   if xudata1(3,i)>maxr-0.00004
   sym1(1,jj)=xudata1(2,i);sym1(2,jj)=xudata1(5,i);
   sym2(1,jj)=xudata1(2,i);sym2(2,jj)=xudata2(5,i);
   jj=jj+1;
   end
      mii=fix((xudata1(2,i)+stp/2)/stp)+1;
   avg1(2,mii)=avg1(2,mii)+xudata1(5,i);
   avg2(2,mii)=avg2(2,mii)+xudata2(5,i);
   if mii>j|i==length(xudata1)
   avg1(2,j)=avg1(2,j)/(i-j2);
   avg2(2,j)=avg2(2,j)/(i-j2);
   i-j2
   j=j+1;j2=i;
   end
end
%绘图
figure(1)
plot(sym1(1,:),sym1(2,:),sym2(1,:),sym2(2,:))
figure(2)
plot(sym1(1,:),sym1(2,:),sym2(1,:),sym2(2,:),avg1(1,:),avg1(2,:),avg2(1,:),avg2(2,:));
legend('sym1','sym2','avg1','avg2');
hold on
plot(sym1(1,:),sym2(2,:)./sym1(2,:),avg1(1,:),avg2(2,:)./avg1(2,:));
legend('symrate','avgrate');
fid=fopen(['tec' name2],'w');
fprintf(fid,'TITLE = "heat transfer coefficient" \nVARIABLES = "x/d", "yta or rate" \nZONE T="sym", I=%d, F=POINT\n',length(sym2));
fprintf(fid,'.6f .6f',sym2);
fprintf(fid,'ZONE T="avg", I=%d, F=POINT\n',length(avg2));
fprintf(fid,'.6f .6f',avg2);
fclose(fid)
%用tecplot格式输出
fid=fopen('tecpost','w');
fprintf(fid,'TITLE = "heat transfer coefficient" \nVARIABLES = "x/d", "yta or rate" \nZONE T="sym", I=%d, F=POINT\n',length(sym1));
fprintf(fid,'.6f .6f',sym1);
fprintf(fid,'ZONE T="avg", I=%d, F=POINT\n',length(avg1));
fprintf(fid,'.6f .6f',avg1);
fclose(fid)

转自:http://blog.sina.com.cn/s/blog_5116499f0100uyn2.html
页: [1]
查看完整版本: matlab读取fluent数据