Skip to content

Call Meshio from Matlab to read and write many mesh formats

License

Notifications You must be signed in to change notification settings

Jimbles/meshio2matlab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

meshio2matlab

Matlab ❤ Meshio

Meshio is a great python library which can read, write and convert unstructured mesh formats. Matlab doesnt have the same support, but through the python interface it is possible to take advantage of Meshio, and prevent having to write and maintain lots of separate I/O functions.

Requirements

  • Python install that Matlab is happy with. Check that pyenv in matlab gives you the correct version.
  • Meshio - pip install meshio[all]
  • Matlab >2014b

This has only been tested on Matlab 2018/2019b with Python 3.7 & 3.8 on Windows 10.

Installation

Add the src folder to matlab path

Usage

Read file

P=meshio.read(fname); % .msh .vtk .vtu .stl .obj TetGen ANSYS ...

% P.vtx             - verticies
% P.Cells           - structure array for each geometry saved in file
% P.Cells.tri       - Trigangulation connectivity list for this cell
% P.Cells.type      - 'Tetra','Triangle','Line','Vertex'
% P.cell_data       - Per element data in cell array
% P.cell_data_name  - Data names in cell array
% P.point_data      - Per point data in cell array
% P.point_data_name - Data names in cell array

% all contents can be plotted using:
meshio.plot(P);

Write file

% random triangulation
x = rand(50,1);
y = rand(50,1);
z= rand(50,1);
dt = delaunayTriangulation(x,y,z);
dataex=1:size(dt.ConnectivityList,1);

%write to gmsh file with cell data
meshio.write('example.msh',dt.Points,dt.ConnectivityList,{dataex},{'Data'});

This can then be loaded directly into gmsh

gmsh

or into paraview (either by writing to .vtu or loading .msh using meshio paraview plugin)

paraview

Convert file

Conversion through meshio-convert binary is harder to fit into matlab workflow, so this library can be used too

P=meshio.read('example.msh');
meshio.structwrite('example.vtu',P);