How to read xml file with binary data into Matlab? (VTK/VTU File)
17 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Richard Crozier
le 4 Août 2016
Modifié(e) : ABDEL MOUMEN
le 26 Mai 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">
<UnstructuredGrid>
<Piece NumberOfPoints="1941" NumberOfCells="11339">
<PointData>
<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"/>
</PointData>
<CellData>
<DataArray type="Int32" Name="GeometryIds" format="appended" offset="139764"/>
</CellData>
<Points>
<DataArray type="Float64" NumberOfComponents="3" format="appended" offset="185124"/>
</Points>
<Cells>
<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"/>
</Cells>
</Piece>
</UnstructuredGrid>
<AppendedData encoding="raw">
_XF@Loû1q@`@!?V7^W@9DCz@bd@Yb@r <snip>
</AppendedData>
</VTKFile>
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).
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;
break;
end
end
Réponse acceptée
Richard Crozier
le 5 Août 2016
4 commentaires
ABDEL MOUMEN
le 26 Mai 2019
Modifié(e) : ABDEL MOUMEN
le 26 Mai 2019
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
Plus de réponses (1)
Zakia Tasnim
le 18 Oct 2018
where should i put the file name?
2 commentaires
Tiago Pestana
le 20 Oct 2018
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
Voir également
Catégories
En savoir plus sur Low-Level File I/O dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!