标签:并保存 initial init open dir ati array find arrays
tecplot按照网格点逐点输出数据到文件,一个点的坐标及其相关数据写成一行,更具数据文件头可以知道,一行含七个数据,分别是x,y,z,u,v,w,rho。
1、matlab读取tecplot文件,将数据存贮到三维数组中,并保存变量到mat文件以供调用
% read data from tecplot file % save the data as the three dimension arrays function [x,y,z,u,v,w,rho]=tecplot2mat(filename,num_head) %% tecplot data file read fid = fopen(strcat(filename,‘.dat‘)); data = textscan(fid,‘%f %f %f %f %f %f %f‘,‘headerlines‘,num_head); data = cell2mat(data); fclose(fid); %% reshape data % get discrete points xi = sort(unique(data(:,1))); yi = sort(unique(data(:,2))); zi = sort(unique(data(:,3))); % number of the discrete points num_x = length(xi); num_y = length(yi); num_z = length(zi); % initialize the three demonsions array x = zeros(num_x,num_y,num_z); y = zeros(num_x,num_y,num_z); z = zeros(num_x,num_y,num_z); u = zeros(num_x,num_y,num_z); v = zeros(num_x,num_y,num_z); w = zeros(num_x,num_y,num_z); rho = zeros(num_x,num_y,num_z); % assignment the array according to the data for n = 1:size(data,1) % % if we don‘t know the relationship between the number and the index,we % % must find the index according to the number % index_x = find(data(n,1) == xi); % index_y = find(data(n,2) == yi); % index_z = find(data(n,3) == zi); % if we know the relationship between the number and the index, we can % directly access the index index_x = data(n,1) + 1; index_y = data(n,2) + 1; index_z = data(n,3) + 1; % access the data x(index_x,index_y,index_z) = data(n,1); y(index_x,index_y,index_z) = data(n,2); z(index_x,index_y,index_z) = data(n,3); u(index_x,index_y,index_z) = data(n,4); v(index_x,index_y,index_z) = data(n,5); w(index_x,index_y,index_z) = data(n,6); rho(index_x,index_y,index_z) = data(n,7); end fprintf(‘reshape the data\n‘); %% data save to mat eval([‘save ‘,filename,‘ x y z u v w rho;‘]); fprintf(‘save the data\n‘); end
2、测试函数
clc;clear; close all; file_info = dir(‘*.dat‘); file_num = length(file_info); num_head = 3; for i = 1:file_num [~,~,~,~,~,~,~]=tecplot2mat(file_info(i).name,num_head); end
标签:并保存 initial init open dir ati array find arrays
原文地址:http://www.cnblogs.com/tonyturtle/p/7845816.html