Effacer les filtres
Effacer les filtres

Read text file and identify certain values line by line, or character by character

2 vues (au cours des 30 derniers jours)
Hi,
sorry for the long post, but I need to perform a specific task on a text-file and were hoping some of you had any good advice on how to assess the problem...
I have a text-file from a meshed figure, indicating vertices positions (x,y,z) and triangular connections (tri), similar to how you give input to Matlabs trimesh e.g. (trimes(tri,x,y,z)). I would like to load this data for further analysis (using e.g. trimesh()).
The data file is though structured as follows:
<?xml version="1.0" encoding="UTF-8"?>
<dolfin xmlns:dolfin="http://www.fenics.org/dolfin/">
<mesh celltype="triangle" dim="3">
<vertices size="3781">
<vertex index="0" x="-0.039642" y="0.121046" z="0.995522" />
<vertex index="1" x="-0.048" y="0.09502" z="0.993072" />
<vertex index="2" x="-0.051299" y="0.11826" z="0.994066" />
...
etc. with lines specifying x,y,z, until after a while the text file changes to:
<vertex index="3777" x="0.03365" y="-0.133752" z="0.07877" />
<vertex index="3778" x="0.015102" y="-0.134083" z="0.07877" />
<vertex index="3779" x="0.15305" y="0.141672" z="1.10581" />
<vertex index="3780" x="0.211239" y="0.159942" z="1.10811" />
</vertices>
<cells size="7558">
<triangle index="0" v0="1" v1="0" v2="2"/>
<triangle index="1" v0="3" v1="4" v2="5"/>
<triangle index="2" v0="7" v1="6" v2="8"/>
<triangle index="3" v0="10" v1="9" v2="11"/>
<triangle index="4" v0="13" v1="12" v2="14"/>
...
when the file then starts going over element indexes with nodes (v0,v1,v3) making up a specific triangular element. The file then continues like this until the end where it looks like
<triangle index="7553" v0="3574" v1="206" v2="3780"/>
<triangle index="7554" v0="206" v1="910" v2="3780"/>
<triangle index="7555" v0="1949" v1="7" v2="1950"/>
<triangle index="7556" v0="2388" v1="3275" v2="2429"/>
<triangle index="7557" v0="84" v1="3637" v2="86"/>
</cells>
</mesh>
</dolfin>
So, the task is to load the data such that I end up with an x,y and z-vector with the x,y,z-values of all nodes, and a separate tri-matrix with the node-element connection (with tri(index) corresponding to the specific index of the element). In principle all my data is given in a straight forward fashion in the text file, but I'm struggling a bit in implementation.
I'm thinking of first reading the file line-by-line and identifying each string character 'x', 'y' and 'z' and then saving the parts in-between that are given inside "-" signs. This looping then somehow have to stop when 'triangle' appears, then the same type of sorting but in matrix form has to start identifying 'v0','v1' and 'v2'. However, reading text files line by line (or character by character) is something I'm not used to in Matlab. Can you do that? Or does anyone have any general ideas on how to approach the problem? Any advice would be very helpful.
Thanks for your help!
/David

Réponse acceptée

per isakson
per isakson le 24 Juin 2015
Modifié(e) : per isakson le 25 Juin 2015
I copy&pasted your XML-file to 'cssm.xml'
>> sas = xml2struct('cssm.xml');
>> sas.dolfin.mesh.cells.triangle{1}.Attributes
ans =
index: '0'
v0: '1'
v1: '0'
v2: '2'
>> sas.dolfin.mesh.cells.triangle{7}.Attributes
ans =
index: '7554'
v0: '206'
v1: '910'
v2: '3780'
>> sas.dolfin.mesh.vertices.vertex{2}.Attributes
ans =
index: '1'
x: '-0.048'
y: '0.09502'
z: '0.993072'
It seems to work
  3 commentaires
David
David le 25 Juin 2015
Answering my own comment since I found sound old threads regarding my follow-up question:
in the above, multiple struct array data can be loaded by e.g:
xml = xml2struct(filename);
x = xml.dolfin.mesh.vertices.vertex;
x = cellfun(@(S) S.Attributes.x, x,'uni',false);
x = [str2double(x')];
etc. But from some initial tests it's not really clear if this is faster than a for-loop structure.
per isakson
per isakson le 25 Juin 2015
Modifié(e) : per isakson le 25 Juin 2015
Matlab for-loops are not as bad as their reputation and they are easy to understand.
These lines might be somewhat faster
cac = xml.dolfin.mesh.vertices.vertex;
num = cellfun(@(S) str2double(S.Attributes.x), cac, 'uni',true);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by