Matlab data save to VTK format

I was looking for some function to save matlab array to VTK data format, and found just what I wanted in this page. They are shown below with some modification on my own:

Scalar data:

function savevtk(array, filename)
%  savevtk Save a 3-D scalar array in VTK format.
%  savevtk(array, filename) saves a 3-D array of any size to
%  filename in VTK format.
    [nx, ny, nz] = size(array);
    fid = fopen(filename, 'wt');
    fprintf(fid, '# vtk DataFile Version 2.0\n');
    fprintf(fid, 'Comment goes here\n');
    fprintf(fid, 'ASCII\n');
    fprintf(fid, '\n');
    fprintf(fid, 'DATASET STRUCTURED_POINTS\n');
    fprintf(fid, 'DIMENSIONS    %d   %d   %d\n', nx, ny, nz);
    fprintf(fid, '\n');
    fprintf(fid, 'ORIGIN    0.000   0.000   0.000\n');
    fprintf(fid, 'SPACING    1.000   1.000   1.000\n');
    fprintf(fid, '\n');
    fprintf(fid, 'POINT_DATA   %d\n', nx*ny*nz);
    fprintf(fid, 'SCALARS scalars double\n');
    fprintf(fid, 'LOOKUP_TABLE default\n');
    fprintf(fid, '\n');
    for a=1:nz
        for b=1:ny
            for c=1:nx
                fprintf(fid, '%d ', array(c,b,a));
            end
            fprintf(fid, '\n');
        end
    end
    fclose(fid);
return

Vector data:

function savevtkvector(X, Y, Z, filename)
%  savevtkvector Save a 3-D vector array in VTK format
%  savevtkvector(X,Y,Z,filename) saves a 3-D vector of any size to
%  filename in VTK format. X, Y and Z should be arrays of the same
%  size, each storing speeds in the a single Cartesian directions.
    if ((size(X) ~= size(Y)) | (size(X) ~= size(Z)))
        fprint('Error: velocity arrays of unequal size\n'); return;
    end
    [nx, ny, nz] = size(X);
    fid = fopen(filename, 'wt');
    fprintf(fid, '# vtk DataFile Version 2.0\n');
    fprintf(fid, 'Comment goes here\n');
    fprintf(fid, 'ASCII\n');
    fprintf(fid, '\n');
    fprintf(fid, 'DATASET STRUCTURED_POINTS\n');
    fprintf(fid, 'DIMENSIONS    %d   %d   %d\n', nx, ny, nz);
    fprintf(fid, '\n');
    fprintf(fid, 'ORIGIN    0.000   0.000   0.000\n');
    fprintf(fid, 'SPACING    1.000   1.000   1.000\n');
    fprintf(fid, '\n');
    fprintf(fid, 'POINT_DATA   %d\n', nx*ny*nz);
    fprintf(fid, 'VECTORS vectors double\n');
    fprintf(fid, '\n');
    for a=1:nz
        for b=1:ny
            for c=1:nx
                fprintf(fid, '%f ', X(c,b,a));
                fprintf(fid, '%f ', Y(c,b,a));
                fprintf(fid, '%f ', Z(c,b,a));
            end
            fprintf(fid, '\n');
        end
    end
    fclose(fid);
return
Advertisements

5 Responses

  1. Do you really nead a loop to write the 3D vector field ? I’ve done without in one go with this : (datax, datay, dataz are 3D vector field)

    [ny,nx,nz]=size(datax);
    xx=1:size(datax,2);
    yy=1:size(datax,1);
    zz=1:size(datax,3);

    datax=datax(:)’;
    datay=datay(:)’;
    dataz=dataz(:)’;

    %% Header
    fid=fopen(fullfile(outputd,filename),’w’);
    fprintf(fid,’%s\n’,’# vtk DataFile Version 3.0′);
    fprintf(fid,’%s\n’,’3D LFF extrapolation’);
    fprintf(fid,’%s\n’,’ASCII’);
    fprintf(fid,’%s\n’,’DATASET RECTILINEAR_GRID’);
    fprintf(fid,’%s %1.0i %1.0i %1.0i\n’,’DIMENSIONS’,nx,ny,nz);
    fprintf(fid,’%s %1.0i %s\n’,’X_COORDINATES’,nx,’float’);
    fprintf(fid,’%1.0i ‘,xx);
    fprintf(fid,’\n%s %1.0i %s\n’,’Y_COORDINATES’,ny,’float’);
    fprintf(fid,’%1.0i ‘,yy);
    fprintf(fid,’\n%s %1.0i %s\n’,’Z_COORDINATES’,nz,’float’);
    fprintf(fid,’%1.0i ‘,zz);

    %% Data
    fprintf(fid,’\n%s %1.0i’,’POINT_DATA’,nx*ny*nz);
    fprintf(fid,’\n%s\n’,’VECTORS BFIELD float’);
    fprintf(fid,’%6.2f %6.2f %6.2f\n’,[datax;datay;dataz]);

    fclose(fid);

  2. I too have created a function to convert matlab array polydata to vtk. But the vtk readers don’t display anything. I don’t know what the problem is. Herre is the sample code. Is there anything obviously wrong? Maybe the points need a reference or initializiation.

    [ny,nx,nz]=size(X);
    fid = fopen(filename, ‘wt’);
    fprintf(fid, ‘# vtk DataFile Version 3.0\n’);
    fprintf(fid, ‘vtk output\n’);
    fprintf(fid, ‘ASCII\n’);
    fprintf(fid, ‘DATASET POLYDATA’);
    fprintf(fid, ‘POINTS %d ‘, nx*ny*nx);
    fprintf(fid, ‘float \n’);
    for a=1:nz
    for b=1:ny
    for c=1:nx
    fprintf(fid, ‘%.3f ‘, X(c,b,a));
    end
    fprintf(fid, ‘\n’);
    end
    end;
    fprintf(fid, ‘LINES %d %d\n’,nz, nx*ny*nz + nz);

    i = 0;
    for k = 1:nz
    fprintf(fid, ‘%d ‘, ny);
    for j = 1:ny
    fprintf(fid, ‘%d ‘, i);
    i = i+1;
    end
    fprintf(fid, ‘\n’);
    end

    fprintf(fid, ‘CELL_DATA %d \n’, nz);
    fprintf(fid, ‘POINT_DATA %d\n’, nx*ny*nz);
    fclose(fid);

  3. Sorry, line 7 of the code above:

    fprintf(fid, ‘POINTS %d ‘, nx*ny*nx);
    should read:
    fprintf(fid, ‘POINTS %d ‘, nx*ny*nz);

  4. Hi Guys,

    Do you mind if I put this code I put this code into gnu/octave forge?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: