Paraview是一个开源的可视化软件。

用到matlab子程序从这里下载

或者到博客末尾复制粘贴

子程序名为 vtkwrite

示例1:

 load mri
D = squeeze(D);
vtkwrite('mri.vtk', 'structured_points', 'mri', D)

示例2:云图

 load wind
[cu,cv,cw] = curl(x, y, z, u, v, w);
div = divergence(x, y, z, u, v, w);
vtkwrite('wind.vtk', 'structured_grid', x, y, z, ...
'vectors', 'vector_field', u, v, w, 'vectors', 'vorticity', cu, cv, cw, 'scalars', 'divergence', div);

示例3:二维曲线

 x = :;
y = sin(x);
z = sqrt(x);
vtkwrite('execute','polydata','lines',x,y,z);

示例4:三角形

 [x,y,z] = peaks();
z = .*z;
tri = delaunay(x,y);
vtkwrite('peaks.vtk','polydata','triangle',x,y,z,tri);

示例5:四面体

 d = [- ];
[x, y, z] = meshgrid(d, d, d);
DT = delaunayTriangulation(x(:), y(:), z(:));
vtkwrite('execute', 'polydata','tetrahedron', x, y, z, DT.ConnectivityList);

vtkwrite

 function vtkwrite( filename,dataType,varargin )
% VTKWRITE Writes 3D Matlab array into VTK file format.
% vtkwrite(filename,'structured_grid',x,y,z,'vectors',title,u,v,w) writes
% a structured 3D vector data into VTK file, with name specified by the string
% filename. (u,v,w) are the vector components at the points (x,y,z). x,y,z
% should be -D matrices like those generated by meshgrid, where
% point(ijk) is specified by x(i,j,k), y(i,j,k) and z(i,j,k).
% The matrices x,y,z,u,v,w must all be the same size and contain
% corrresponding position and vector component. The string title specifies
% the name of the vector field to be saved.
%
% vtkwrite(filename,'structured_grid',x,y,z,'scalars',title,r) writes a 3D
% scalar data into VTK file whose name is specified by the string
% filename. r is the scalar value at the points (x,y,z). The matrices
% x,y,z,r must all be the same size and contain the corresponding position
% and scalar values.
%
% vtkwrite(filename,'structured_grid',x,y,z,'vectors',title,u,v,w,'scalars',
% title2,r) writes a 3D structured grid that contains both vector and scalar values.
% x,y,z,u,v,w,r must all be the same size and contain the corresponding
% positon, vector and scalar values.
%
% vtkwrite(filename, 'structured_points', title, m) saves matrix m (could
% be 1D, 2D or 3D array) into vtk as structured points.
%
% vtkwrite(filename, 'structured_points', title, m, 'spacing', sx, sy, sz)
% allows user to specify spacing. (default: , , ). This is the aspect
% ratio of a single voxel.
%
% vtkwrite(filename, 'structured_points', title, m, 'origin', ox, oy, oz)
% allows user to speicify origin of dataset. (default: , , ).
%
% vtkwrite(filename,'unstructured_grid',x,y,z,'vectors',title,u,v,w,'scalars',
% title2,r) writes a 3D unstructured grid that contains both vector and scalar values.
% x,y,z,u,v,w,r must all be the same size and contain the corresponding
% positon, vector and scalar values.
%
% vtkwrite(filename, 'polydata', 'lines', x, y, z) exports a 3D line where
% x,y,z are coordinates of the points that make the line. x, y, z are
% vectors containing the coordinates of points of the line, where point(n)
% is specified by x(n), y(n) and z(n).
%
% vtkwrite(filename,'polydata','lines',x,y,z,'Precision',n) allows you to
% specify precision of the exported number up to n digits after decimal
% point. Default precision is digits.
%
% vtkwrite(filename,'polydata','triangle',x,y,z,tri) exports a list of
% triangles where x,y,z are the coordinates of the points and tri is an
% m* matrix whose rows denote the points of the individual triangles.
%
% vtkwrite(filename,'polydata','tetrahedron',x,y,z,tetra) exports a list
% of tetrahedrons where x,y,z are the coordinates of the points
% and tetra is an m* matrix whose rows denote the points of individual
% tetrahedrons.
%
% vtkwrite('execute','polydata','lines',x,y,z) will save data with default
% filename ''matlab_export.vtk' and automatically loads data into
% ParaView.
%
% Version 2.3
% Copyright, Chaoyuan Yeh,
% Codes are modified from William Thielicke and David Gingras's submission. if strcmpi(filename,'execute'), filename = 'matlab_export.vtk'; end
fid = fopen(filename, 'w');
% VTK files contain five major parts
% . VTK DataFile Version
fprintf(fid, '# vtk DataFile Version 2.0\n');
% . Title
fprintf(fid, 'VTK from Matlab\n'); binaryflag = any(strcmpi(varargin, 'BINARY'));
if any(strcmpi(varargin, 'PRECISION'))
precision = num2str(varargin{find(strcmpi(vin, 'PRECISION'))+});
else
precision = '';
end switch upper(dataType)
case 'STRUCTURED_POINTS'
title = varargin{};
m = varargin{};
if any(strcmpi(varargin, 'spacing'))
sx = varargin{find(strcmpi(varargin, 'spacing'))+};
sy = varargin{find(strcmpi(varargin, 'spacing'))+};
sz = varargin{find(strcmpi(varargin, 'spacing'))+};
else
sx = ;
sy = ;
sz = ;
end
if any(strcmpi(varargin, 'origin'))
ox = varargin{find(strcmpi(varargin, 'origin'))+};
oy = varargin{find(strcmpi(varargin, 'origin'))+};
oz = varargin{find(strcmpi(varargin, 'origin'))+};
else
ox = ;
oy = ;
oz = ;
end
[nx, ny, nz] = size(m);
setdataformat(fid, binaryflag); fprintf(fid, 'DATASET STRUCTURED_POINTS\n');
fprintf(fid, 'DIMENSIONS %d %d %d\n', nx, ny, nz);
fprintf(fid, ['SPACING ', num2str(sx), ' ', num2str(sy), ' ',...
num2str(sz), '\n']);
fprintf(fid, ['ORIGIN ', num2str(ox), ' ', num2str(oy), ' ',...
num2str(oz), '\n']);
fprintf(fid, 'POINT_DATA %d\n', nx*ny*nz);
fprintf(fid, ['SCALARS ', title, ' float 1\n']);
fprintf(fid,'LOOKUP_TABLE default\n');
if ~binaryflag
spec = ['%0.', precision, 'f '];
fprintf(fid, spec, m(:)');
else
fwrite(fid, m(:)', 'float', 'b');
end case {'STRUCTURED_GRID','UNSTRUCTURED_GRID'}
% . The format data proper is saved in (ASCII or Binary). Use
% fprintf to write data in the case of ASCII and fwrite for binary.
if numel(varargin)<, error('Not enough input arguments'); end
setdataformat(fid, binaryflag);
% fprintf(fid, 'BINARY\n');
x = varargin{};
y = varargin{};
z = varargin{};
if sum(size(x)==size(y) & size(y)==size(z))~=length(size(x))
error('Input dimesions do not match')
end
n_elements = numel(x);
% . Type of Dataset ( can be STRUCTURED_POINTS, STRUCTURED_GRID,
% UNSTRUCTURED_GRID, POLYDATA, RECTILINEAR_GRID or FIELD )
% This part, dataset structure, begins with a line containing the
% keyword 'DATASET' followed by a keyword describing the type of dataset.
% Then the geomettry part describes geometry and topology of the dataset.
if strcmpi(dataType,'STRUCTURED_GRID')
fprintf(fid, 'DATASET STRUCTURED_GRID\n');
fprintf(fid, 'DIMENSIONS %d %d %d\n', size(x,), size(x,), size(x,));
else
fprintf(fid, 'DATASET UNSTRUCTURED_GRID\n');
end
fprintf(fid, ['POINTS ' num2str(n_elements) ' float\n']);
output = [x(:)'; y(:)'; z(:)']; if ~binaryflag
spec = ['%0.', precision, 'f '];
fprintf(fid, spec, output);
else
fwrite(fid, output, 'float', 'b');
end
% .This final part describe the dataset attributes and begins with the
% keywords 'POINT_DATA' or 'CELL_DATA', followed by an integer number
% specifying the number of points of cells. Other keyword/data combination
% then define the actual dataset attribute values.
fprintf(fid, ['\nPOINT_DATA ' num2str(n_elements)]);
% Parse remaining argument.
vidx = find(strcmpi(varargin,'VECTORS'));
sidx = find(strcmpi(varargin,'SCALARS'));
if vidx~=
for ii = :length(vidx)
title = varargin{vidx(ii)+};
% Data enteries begin with a keyword specifying data type
% and numeric format.
fprintf(fid, ['\nVECTORS ', title,' float\n']);
output = [varargin{ vidx(ii) + }(:)';...
varargin{ vidx(ii) + }(:)';...
varargin{ vidx(ii) + }(:)']; if ~binaryflag
spec = ['%0.', precision, 'f '];
fprintf(fid, spec, output);
else
fwrite(fid, output, 'float', 'b');
end
% fwrite(fid, [reshape(varargin{vidx(ii)+},,n_elements);...
% reshape(varargin{vidx(ii)+},,n_elements);...
% reshape(varargin{vidx(ii)+},,n_elements)],'float','b');
end
end
if sidx~=
for ii = :length(sidx)
title = varargin{sidx(ii)+};
fprintf(fid, ['\nSCALARS ', title,' float\n']);
fprintf(fid, 'LOOKUP_TABLE default\n');
if ~binaryflag
spec = ['%0.', precision, 'f '];
fprintf(fid, spec, varargin{ sidx(ii) + });
else
fwrite(fid, varargin{ sidx(ii) + }, 'float', 'b');
end
% fwrite(fid, reshape(varargin{sidx(ii)+},,n_elements),'float','b');
end
end case 'POLYDATA' fprintf(fid, 'ASCII\n');
if numel(varargin)<, error('Not enough input arguments'); end
x = varargin{}(:);
y = varargin{}(:);
z = varargin{}(:);
if numel(varargin)<, error('Not enough input arguments'); end
if sum(size(x)==size(y) & size(y)==size(z))~= length(size(x))
error('Input dimesions do not match')
end
n_elements = numel(x);
fprintf(fid, 'DATASET POLYDATA\n');
if mod(n_elements,)==
x(n_elements+:n_elements+,)=[;];
y(n_elements+:n_elements+,)=[;];
z(n_elements+:n_elements+,)=[;];
elseif mod(n_elements,)==
x(n_elements+,)=;
y(n_elements+,)=;
z(n_elements+,)=;
end
nbpoint = numel(x);
fprintf(fid, ['POINTS ' num2str(nbpoint) ' float\n']); spec = [repmat(['%0.', precision, 'f '], , ), '\n']; output = [x(::end-), y(::end-), z(::end-),...
x(::end-), y(::end-), z(::end-),...
x(::end), y(::end), z(::end)]'; fprintf(fid, spec, output); switch upper(varargin{})
case 'LINES'
if mod(n_elements,)==
nbLine = *n_elements-;
else
nbLine = *(n_elements-);
end
conn1 = zeros(nbLine,);
conn2 = zeros(nbLine,);
conn2(:nbLine/) = :nbLine/;
conn1(:nbLine/) = conn2(:nbLine/)-;
conn1(nbLine/+:end) = :nbLine/;
conn2(nbLine/+:end) = conn1(nbLine/+:end)-;
fprintf(fid,'\nLINES %d %d\n',nbLine,*nbLine);
fprintf(fid,'2 %d %d\n',[conn1';conn2']);
case 'TRIANGLE'
ntri = length(varargin{});
fprintf(fid,'\nPOLYGONS %d %d\n',ntri,*ntri);
fprintf(fid,'3 %d %d %d\n',(varargin{}-)');
case 'TETRAHEDRON'
ntetra = length(varargin{});
fprintf(fid,'\nPOLYGONS %d %d\n',ntetra,*ntetra);
fprintf(fid,'4 %d %d %d %d\n',(varargin{}-)');
end
end
fclose(fid);
if strcmpi(filename,'matlab_export.vtk')
switch computer
case {'PCWIN','PCWIN64'}
!paraview.exe --data='matlab_export.vtk' &
% Exclamation point character is a shell escape, the rest of the
% input line will be sent to operating system. It can not take
% variables, though. The & at the end of line will return control to
% Matlab even when the outside process is still running.
case {'GLNXA64','MACI64'}
!paraview --data='matlab_export.vtk' &
end
end
end function setdataformat(fid, binaryflag) if ~binaryflag
fprintf(fid, 'ASCII\n');
else
fprintf(fid, 'BINARY\n');
end
end

Matlab处理数据导出Paraview可读的vtk文件(一)的更多相关文章

  1. Matlab处理数据导出Paraview可读的vtk文件(二)

    由于我在用SPH方法仿真时用的是FORTRAN语言,并且没有找到直接输出vtk文件的代码,因此偷懒通过MATLAB转换一下数据. 用到的Matlab子程序可通过一下链接找到. Matlab处理数据导出 ...

  2. 利用PHPExcel将数据导出到xls格式的excel文件

    在开发某地的经营许可证管理系统的时候需要将数据导出打excel文件,虽然一年前做某集团的ERP的时候用到过一次导入和导出,但是那时候太忙没时间写博客,一年过去了我也忘的差不多了,所以趁着今天将此次的使 ...

  3. 彻底理解使用JavaScript 将Json数据导出CSV文件

    前言 将数据报表导出,是web数据报告展示常用的附带功能.通常这种功能都是用后端开发人员编写的.今天我们主要讲的是直接通过前端js将数据导出Excel的CSV格式的文件. 原理 首先在本地用Excel ...

  4. MATLAB 的数据导入与导出

    1 数据导入: %% 高层次读取数据. importdata 函数是一个高层次的函数 filename = 'weeklydata.txt'; delimiterIn =' '; %delimiter ...

  5. dataview将excel表格的数据导出成txt文件

    有时候需要处理大量的数据,且这些数据又存在于excel表格内,在平时的时候,我是非常喜欢这样的数据的,因为只要是excel表格内的数据,处理起来的方法就很方便.也可能我平时遇见的数据总是以一种杂乱无章 ...

  6. 【matlab】将matlab中数据输出保存为txt或dat格式

    将matlab中数据输出保存为txt或dat格式 总结网上各大论坛,主要有三种方法. 第一种方法:save(最简单基本的) 具体的命令是:用save *.txt -ascii x x为变量 *.txt ...

  7. .net解决数据导出excel时的格式问题

    在项目中一般都需要将报表数据导出到EXCEL中,但经常出现导出长串数据(如身份证)到EXCEL中后显示为科学计数法的格式,或者报表中显示为001的数据导出到Excel后成了1的格式. 下面简单介绍一下 ...

  8. Winform数据导出Execl小工具

    前台界面.cs文件 using System; using System.Collections.Generic; using System.ComponentModel; using System. ...

  9. 数据导出至Excel文件--好库编程网http://code1.okbase.net/codefile/SerializeHelper.cs_2012122018724_118.htm

    using System; using System.IO; using System.Data; using System.Collections; using System.Data.OleDb; ...

随机推荐

  1. 2115: [Wc2011] Xor (线性基+dfs)

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 5714  Solved: 2420 题目链接:https://w ...

  2. Java并发编程学习笔记

    Java编程思想,并发编程学习笔记. 一.基本的线程机制 1.定义任务:Runnable接口 线程可以驱动任务,因此需要一种描述任务的方式,这可以由Runnable接口来提供.要想定义任务,只需实现R ...

  3. Android JUnit test

    Android单元测试步骤 1.修改AndroidManifest.xml文件. 添加instrumentation节点.其中name是固定值,targetPackage为需要测试的类所在的包.如:  ...

  4. HDU 4529 状压dp

    郑厂长系列故事——N骑士问题 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)To ...

  5. POJ3186 DP

    Treats for the Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5753   Accepted: 29 ...

  6. 主机不能访问虚拟机CentOS中的站点

    主机能ping通虚拟机 虚拟机也能ping通主机 主机不能telenet通虚拟机 原因:虚拟机开启了防火墙, 解决办法:关闭虚拟机防火墙. Centos 7 firewall 命令: 查看已经开放的端 ...

  7. JS设计模式之装饰者模式

    装饰者模式概述 在不改变原对象的基础上,通过对其进行包装拓展(添加属性或者方法)使原有对象可以满足用户更复杂的需求 实际需求 在已有的代码基础上,为每个表单中的input默认输入框上边显示一行提示文案 ...

  8. ZooKeeper概述(三)

    ZooKeeper:分布式应用的分布协调服务 ZooKeeper是一个为分布式应用提供的分布的开源的协调服务.它暴露一组简单的原子操作,分布式系统可以在这之上为同步,配置管理,和组和命名实现更高级的服 ...

  9. 【TYVJ】1520 树的直径

    [算法]树的直径 memset(a,0,sizeof(a)) #include<cstdio> #include<algorithm> #include<cstring& ...

  10. Machine Learning(CF940F+带修改莫队)

    题目链接:http://codeforces.com/problemset/problem/940/F 题目: 题意:求次数的mex,mex的含义为某个集合(如{1,2,4,5})第一个为出现的非负数 ...