# Write Binary UnstructuredGrid CellData LegacyVTK in Matlab

**URL:** https://discourse.paraview.org/t/write-binary-unstructuredgrid-celldata-legacyvtk-in-matlab/776
**Category:** ParaView Support
**Created:** [October 22, 2018, 12:31pm UTC](https://discourse.paraview.org/t/write-binary-unstructuredgrid-celldata-legacyvtk-in-matlab/776 "2018-10-22T12:31:13Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Jimbles](https://discourse.paraview.org/user_avatar/discourse.paraview.org/jimbles/32/474_2.png) [@Jimbles](https://discourse.paraview.org/u/Jimbles)
#### Post date: [October 22, 2018, 12:31pm UTC](https://discourse.paraview.org/t/write-binary-unstructuredgrid-celldata-legacyvtk-in-matlab/776/1 "2018-10-22T12:31:14Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.paraview.org/user_avatar/discourse.paraview.org/banesullivan/32/11714_2.png) [@banesullivan](https://discourse.paraview.org/u/banesullivan)
#### Post date: [October 22, 2018, 2:30pm UTC](https://discourse.paraview.org/t/write-binary-unstructuredgrid-celldata-legacyvtk-in-matlab/776/2 "2018-10-22T14:30:46Z")

</div>

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`](https://github.com/nschloe/meshio).

---

<div class="post-metadata">

### Author: ![Jimbles](https://discourse.paraview.org/user_avatar/discourse.paraview.org/jimbles/32/474_2.png) [@Jimbles](https://discourse.paraview.org/u/Jimbles)
#### Post date: [October 22, 2018, 3:23pm UTC](https://discourse.paraview.org/t/write-binary-unstructuredgrid-celldata-legacyvtk-in-matlab/776/3 "2018-10-22T15:23:23Z")

</div>

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](https://discourse.paraview.org/uploads/default/original/1X/c19f9ba7e4890de27b791c5f0728c1f1a3c6c25a.m) (1.6 KB)

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.paraview.org/user_avatar/discourse.paraview.org/banesullivan/32/11714_2.png) [@banesullivan](https://discourse.paraview.org/u/banesullivan)
#### Post date: [October 22, 2018, 3:43pm UTC](https://discourse.paraview.org/t/write-binary-unstructuredgrid-celldata-legacyvtk-in-matlab/776/4 "2018-10-22T15:43:08Z")

</div>

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

```auto
fwrite(FID, stuff, 'float');

```

isntead of: (I don’t think you need the `'b'` flag since [`fwrite`](https://www.mathworks.com/help/matlab/ref/fwrite.html) already defaults to binary? (my MatLab knowledge is very rusty)

```auto
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…~~

---

<div class="post-metadata">

### Author: ![Jimbles](https://discourse.paraview.org/user_avatar/discourse.paraview.org/jimbles/32/474_2.png) [@Jimbles](https://discourse.paraview.org/u/Jimbles)
#### Post date: [October 22, 2018, 4:20pm UTC](https://discourse.paraview.org/t/write-binary-unstructuredgrid-celldata-legacyvtk-in-matlab/776/5 "2018-10-22T16:20:58Z")

</div>

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

> <https://gist.github.com/Jimbles/22fd4bdb49d808747a7e8dd9c6e9e8dc>

  
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.  

> **[Matlab 3D vectors to Paraview (via binary VTK file)](https://uk.mathworks.com/matlabcentral/fileexchange/34310-matlab-3d-vectors-to-paraview-via-binary-vtk-file?s_tid=FX_rc3_behav)**
>
> Writes a 3D (x,y,z,u,v,w) vector field to a binary VTK file for Paraview.

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

Thanks again

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.paraview.org/user_avatar/discourse.paraview.org/banesullivan/32/11714_2.png) [@banesullivan](https://discourse.paraview.org/u/banesullivan)
#### Post date: [October 23, 2018, 1:40am UTC](https://discourse.paraview.org/t/write-binary-unstructuredgrid-celldata-legacyvtk-in-matlab/776/6 "2018-10-23T01:40:31Z")

</div>

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](https://discourse.paraview.org/uploads/default/original/1X/c95f2db26c75cd0494dd0b9d850eeb41efb24e80.m) (1.9 KB)

---

<div class="post-metadata">

### Author: ![Jimbles](https://discourse.paraview.org/user_avatar/discourse.paraview.org/jimbles/32/474_2.png) [@Jimbles](https://discourse.paraview.org/u/Jimbles)
#### Post date: [October 23, 2018, 9:09am UTC](https://discourse.paraview.org/t/write-binary-unstructuredgrid-celldata-legacyvtk-in-matlab/776/7 "2018-10-23T09:09:30Z")

</div>

🤦‍♂️🤦‍♂️🤦‍♂️🤦‍♂️🤦‍♂️  
Doh! How silly of me - I should have noticed the `%d` in the fprintf statements. Thanks so much, its very much appreciated 🙂

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.paraview.org/user_avatar/discourse.paraview.org/banesullivan/32/11714_2.png) [@banesullivan](https://discourse.paraview.org/u/banesullivan)
#### Post date: [October 23, 2018, 5:38pm UTC](https://discourse.paraview.org/t/write-binary-unstructuredgrid-celldata-legacyvtk-in-matlab/776/8 "2018-10-23T17:38:26Z")

</div>

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)
