Use regular expression to extract a matrix from a file

조회 수: 1 (최근 30일)
Gabriel
Gabriel 2018년 5월 23일
답변: dpb 2018년 5월 23일
Dear all,
I've been trying to read a matrix from a (very confusing) file, as shown below.
I am using MATLAB R2017a and the textread function with '%f%f%f%f' to read the "$Nodes" to "$EndNodes" numbers, but I had no success. What is the correct mask format to read this file as I need? More precisely, I need to extract the matrix from [1 0 0 0] to [12 0.3939339828220179 0.6060660171779821 0] in this example.
$MeshFormat
2.2 0 8
$EndMeshFormat
$Nodes
24
1 0 0 0
2 1 0 0
3 1 1 0
4 0 1 0
5 0.35 0.5 0
6 0.65 0.5 0
7 0.5 0.35 0
8 0.5 0.65 0
9 0.3939339828220177 0.393933982822018 0
10 0.6060660171779819 0.3939339828220176 0
11 0.6060660171779821 0.6060660171779821 0
12 0.3939339828220179 0.6060660171779821 0
$EndNodes
$Elements
46
1 1 2 3 1 1 2
2 1 2 4 2 2 3
3 1 2 5 3 3 4
4 1 2 4 4 4 1
5 2 2 1 1 6 20 17
6 2 2 1 1 7 15 13
7 2 2 1 1 9 14 15
$EndElements

채택된 답변

dpb
dpb 2018년 5월 23일
>> fid=fopen('gabe.txt');
>> a=cell2mat(textscan(fid,repmat('%f',1,4),'headerlines',5,'collectoutput',1))
a =
1.0000 0 0 0
2.0000 1.0000 0 0
3.0000 1.0000 1.0000 0
4.0000 0 1.0000 0
5.0000 0.3500 0.5000 0
6.0000 0.6500 0.5000 0
7.0000 0.5000 0.3500 0
8.0000 0.5000 0.6500 0
9.0000 0.3939 0.3939 0
10.0000 0.6061 0.3939 0
11.0000 0.6061 0.6061 0
12.0000 0.3939 0.6061 0
>> fid=fclose(fid);
If you don't know where the first record of interest is, you can find it...
fid=fopen('gabe.txt');
while isempty(strfind(fgetl(fid),'$Nodes')),end
a=cell2mat(textscan(fid,repmat('%f',1,4),'headerlines',1,'collectoutput',1));

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by