Write Binary UnstructuredGrid CellData LegacyVTK in Matlab

Hey there,
I have been using the function writeVTKcell.m to write tetrahedral mesh files to legacy VTK format, but I would like to try and make this process more efficient by switching to binary files. However, it is obviously not as simple as swapping fprintf to fwrite as Paraview crashes when loading my new VTK generated files. Does anyone have an example of doing this that I can compare to?
Thanks in advance!

Can you provide a link to writeVTKcell.m for reference? I think it would be a simple as specifying a few keywords in the XML and then properly packing the data values.

I don’t have a good solution on hand for you at the moment, but I would be very wary using VTK file I/O scripts not directly from the VTK library. If your “tetrahedral mesh” has a generic structure, I’d recommend using VTK’s Python wrappings to convert it to a VTK data object and then write it out. Or maybe check out meshio.

1 Like

Sorry, including that would have made it easier! I have included a simple working example of what I am trying to achieve in MATLAB. It is using the legacy VTK format, as opposed to the XML format. In reality, I am using meshes of around 200k - 2M elements and I would like to be able to load timeseries of a few hundred steps. Having a separate ASCII VTK for this is cumbersome, so I figured the first step would be to move to binary files, then work out how to save multiple time points in a single file, to avoid having to define the POINTS and CELLS over and over.

Meshio looks amazing, and is something I’ll use in the future, but unfortunately I havent found anything similar for MATLAB, which I am kind of stuck with at the moment.

Thanks for your help

writevtkscript.m (1.6 KB)

I can’t run your script to test this without some test data, but the Binary part looks generally good. Have you tried using:

fwrite(FID, stuff, 'float');

isntead of: (I don’t think you need the 'b' flag since fwrite already defaults to binary? (my MatLab knowledge is very rusty)

fwrite(FID, stuff, 'float', 'b');

I remember from about a year ago, I was doing similar binary file I/O in MatLab and there being something finicky about fwrite. If you send me a script to create some dummy input data, I could try to test this.

And define the data type as double not float as VTK/ParaView might not know how to handle float data types in the legacy format? Not sure about this…

1 Like

Sorry! delaunay(X) in the second line should be delauney(Nodes), then everything is made from the script. I have uploaded the latest version.


This exact example from the mathworks exchanges works for me, but I cant see what the difference is tbh. Just focusing on the Points definition, this seems to write multiple lines, whereas all my binary outputs appear on a single one.

I think you’re right about fwrite being the problem, fwrite(FID,Data,'float','b' is to specify Big-endian ordering

Thanks again

1 Like

I was a bit busy today but finally had a chance to actually run the MatLab script you supplied. You need to make sure that cell types and cell indices are written as integer binary arrays (not floats). This updated script works as expected:

writevtkscript.m (1.9 KB)

1 Like

:man_facepalming::man_facepalming::man_facepalming::man_facepalming::man_facepalming:
Doh! How silly of me - I should have noticed the %d in the fprintf statements. Thanks so much, its very much appreciated :slight_smile:

1 Like

Happy to help! If that modified script provides a solution for you, please mark that post as the solution! (And update the Gist for others to have in the future)