heavycareer

Matlab Read Vtk File Format

Permalink

Join GitHub today

GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.

Sign up
Branch:master
Find file Copy path
1 contributor
function [vertex,face] = read_vtk(filename, verbose)
% read_vtk - read data from VTK file.
%
% [vertex,face] = read_vtk(filename, verbose);
%
% 'vertex' is a 'nb.vert x 3' array specifying the position of the vertices.
% 'face' is a 'nb.face x 3' array specifying the connectivity of the mesh.
%
% Copyright (c) Mario Richtsfeld
ifnargin<2
verbose = 1;
end
fid = fopen(filename,'r');
if( fid-1 )
error('Can't open the file.');
return;
end
str = fgets(fid); % -1 if eof
if ~strcmp(str(3:5), 'vtk')
error('The file is not a valid VTK one.');
end
%%% read header %%%
str = fgets(fid);
str = fgets(fid);
str = fgets(fid);
str = fgets(fid);
nvert = sscanf(str,'%*s %d %*s', 1);
% read vertices
[A,cnt] = fscanf(fid,'%f%f%f', 3*nvert);
if cnt~=3*nvert
warning('Problem in reading vertices.');
end
A = reshape(A, 3, cnt/3);
vertex = A;
% read polygons
str = fgets(fid);
str = fgets(fid);
info = sscanf(str,'%c %*s %*s', 1);
if((info ~='P') && (info ~='V'))
str = fgets(fid);
info = sscanf(str,'%c %*s %*s', 1);
end
if(info 'P')
nface = sscanf(str,'%*s %d %*s', 1);
[A,cnt] = fscanf(fid,'%d%d%d%dn', 4*nface);
if cnt~=4*nface
warning('Problem in reading faces.');
end
A = reshape(A, 4, cnt/4);
face = A(2:4,:)+1;
end
if(info ~='P')
face = 0;
end
% read vertex ids
if(info 'V')
nv = sscanf(str,'%*s %d %*s', 1);
[A,cnt] = fscanf(fid,'%d%dn', 2*nv);
if cnt~=2*nv
warning('Problem in reading faces.');
end
A = reshape(A, 2, cnt/2);
face = repmat(A(2,:)+1, 3, 1);
end
if((info ~='P') && (info ~='V'))
face = 0;
end
fclose(fid);
return
  • Copy lines
  • Copy permalink

Actually, i just modified the file to be more general to scalar data, using signed, floating points. No need to normalize, no need to have positive values :

For that, one can simply change ' SCALARS blah char '
to 'SCALARS blah float'.
Then instead of :
fwrite(fid, num2str(v'));

use, for instance with a precision of 3 digits :
fprintf(fid,'%1.3f040',v); Lastxp v16.2.3 dvd-iso.

which will add only one blank space between each value whether it is positive or negative. (fwrite would actually add 2 blanks if the value is positive, fprintf understands the 040 ascii code as it's supposed to).

So, finally, we'd have :

fwrite(fid, ['# vtk DataFile Version 2.0' nl 'Volume example' nl 'ASCII' nl ..
'DATASET STRUCTURED_POINTS' nl 'DIMENSIONS ' ..
num2str(N) ' ' num2str(M) ' ' num2str(O) nl 'ASPECT_RATIO 1 1 1' nl ..
'ORIGIN 0 0 0' nl 'POINT_DATA ' ..
num2str(N*M*O) nl 'SCALARS matlab_scalars float 1' nl 'LOOKUP_TABLE default' nl]);

The Simply Austin Channel is a unique blend cutting edge video tutorials on some of the most entertaining systems and programs out there. Epsxe plugins pack. The channel began in April 2015 on Youtube and exploded in popularity.

Powerpoint. for z = 1:O
% Get this layer.
v = matrix(:, :, z)';
% Scale it. This assumes there are no negative numbers. I'm not sure
% this is actually necessary.
v = v(:)';
% Write the values as text numbers.
fprintf(fid,'%1.3f040',v);
% Newline.
fwrite(fid, nl);
end

Tested with Paraview 4.1. Worked like charm.