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