MATLAB Answers


How to read xml file with binary data into Matlab? (VTK/VTU File)

Asked by Richard Crozier on 4 Aug 2016
Latest activity Edited by ABDEL MOUMEN on 26 May 2019
First of all, I've also asked this question on another site but not had any luck, so I thought I'd try here too. I'll cross-post any answer to either site.
I would like to be able to read in an xml formal file which has a section containing binary data. An example file is shown below:
<?xml version="1.0"?>
<VTKFile type="UnstructuredGrid" version="0.1" byte_order="LittleEndian">
<Piece NumberOfPoints="1941" NumberOfCells="11339">
<DataArray type="Float64" Name="magnetic field strength" NumberOfComponents="3" format="appended" offset="0"/>
<DataArray type="Float64" Name="magnetic flux density" NumberOfComponents="3" format="appended" offset="46588"/>
<DataArray type="Float64" Name="magnetic vector potential" NumberOfComponents="3" format="appended" offset="93176"/>
<DataArray type="Int32" Name="GeometryIds" format="appended" offset="139764"/>
<DataArray type="Float64" NumberOfComponents="3" format="appended" offset="185124"/>
<DataArray type="Int32" Name="connectivity" format="appended" offset="231712"/>
<DataArray type="Int32" Name="offsets" format="appended" offset="403396"/>
<DataArray type="Int32" Name="types" format="appended" offset="448756"/>
<AppendedData encoding="raw">
_XF@Loû1q@`@!?V7^W@9DCz@bd@Yb@r <snip>
This is a VTK data file, specifically the unstructured gid type, for which the .vtu extension is used. The format of this is normal xml, but with a section AppendedData where there is an underscore followed by binary data, the xml describes where each of the data sequences start and end in this data.
Matlab's xmlread can't read this file, I presume because of the binary portion. I get the error below:
[Fatal Error] elmer_3d_magnet_mesh.dat0001.vtu:24:1: Invalid byte 1 of 1-byte UTF-8 sequence.
Error using xmlread (line 97)
Java exception occurred:
org.xml.sax.SAXParseException; systemId: file:/home/rcrozier/Sync/cad_models/elmer_3D_magnet/elmer_3d_magnet_mesh/elmer_3d_magnet_mesh.dat0001.vtu; lineNumber: 24;
columnNumber: 1; Invalid byte 1 of 1-byte UTF-8 sequence.
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
However, I can successfully read in the xml portion of the file (using fgetl to read up to the AppendedData tag). I can then create a temporary xml file by adding the missing closing tags and reading this in using xmlread. I can then parse the xml to determine the data structure. This just leaves the reading in the binary portion part. At the end of reading the xml data fgetl leaves me at the file position corresponding to the start of the line with the underscore.
How can I ignore the underscore character, then read in the binary data?
Actually it is the 'ignoring the underscore character' part that is proving difficult as I can't figure out out how to do this without knowing about the character encoding of the file (file -bi returns application/xml; charset=binary on one example).
In case it's of interest, the actual vtk file format specification can be found here (pdf)
Below is the code to get the first text xml part of the file with fgetl
% open the file
fid = fopen(filename, 'r');
% close file when we're done
CC = onCleanup (@() fclose(fid));
xmlstrs = {fgetl(fid)};
find = 1;
while ischar (xmlstrs{find})
find = find + 1;
xmlstrs{find,1} = fgetl(fid);
if ~isempty(strfind (xmlstrs{find,1}, 'AppendedData'))
xmlstrs = [ xmlstrs; {'</AppendedData>'; '</VTKFile>'} ];
% could get file position like this? how many bytes?
datapos = ftell (fid) + 4;

  1 Comment

I should add that solutions which require mex files are acceptable!

Sign in to comment.


2 Answers

Answer by Richard Crozier on 5 Aug 2016
 Accepted Answer

The answer to determining the position was to calculate the character bit length from the first line read in like so:
% open the file
fid = fopen(filename, 'r');
% close file when we're done
CC = onCleanup (@() fclose(fid));
xmlstrs = {fgetl(fid)};
firstlinebytes = ftell (fid) - 1;
bytesperchar = round (firstlinebytes / numel (xmlstrs{1}));
then the position of the first byte in the data section is
datapos = ftell (fid) + bytesperchar;
Note, that this isn't the whole answer to reading 'raw' type data in the AppendedData section which is poorly documented. You will find more info on the format of 'raw' (rather than 'base64') data here, but the short answer is it's encoded like the following:
^ ^ ^
1 2 3
where each "NNNN" is an unsigned 32-bit integer, and <data> consists of
a number of bytes equal to the preceding NNNN value. The corresponding
DataArray elements must have format="appended" and offset attributes
equal to the following:
1.) offset="0"
2.) offset="(4+NNNN1)"
3.) offset="(4+NNNN1+4+NNNN2)"


Show 1 older comment
Thanks for posting a function! However, I was trying to use your code and it doesn't recognize the function "contains". Is that one of your own functions, or is it part of a toolbox that I don't have?
contains is a recent matlab addition
equivalent for older matlab is
isempty (strfind (TEXT,PATTERN))
Hi Richard,
Thanks for your time and for sharing,
I used the function cited belllow to extract a scalar field from pvtu file but it returns the following error :
Error using str2num (line 35)
Input must be a character vector or string scalar.
Error in read_vtkpoly>get_int (line 48)
myint = str2num(regexp(fline{line_nb},regex_str,'match','once'));
Error in read_vtkpoly (line 23)
num_vertices = get_int(fline2,'NumberOfPoints');
Could you please help me to fix that?
Thanks again

Sign in to comment.

Answer by Zakia Tasnim on 18 Oct 2018

where should i put the file name?


function [v,f,s] = read_vtkpoly(fname,scalar_name)
v are the verticies f the faces s is the scalar field you want to retrieve
fname is the file name scalar_name is the variable name to be read
Why have you put this as an answer to the question???

Sign in to comment.