VisitMatlab: read2DScalarVTK.m

File read2DScalarVTK.m, 1.3 KB (added by Jonathan, 7 years ago)
Line 
1function data=read2DScalarVTK(filename)
2fid=fopen(filename);
3% search for POINTS Section
4line=fsearch(fid,'POINTS');
5npoints=sscanf(line,'POINTS %d');
6
7% Read in points
8data.Vertices=zeros(3,npoints);
9data.Vertices(:)=fscanf(fid,'%f',npoints*3);
10
11% Search for POLYGONS section
12line=fsearch(fid,'POLYGONS');
13npolygons=sscanf(line,'POLYGONS %d');
14
15% read in POLYGON data
16data.Faces=zeros(4,npolygons,'int64');
17data.Faces(:)=fscanf(fid,'%d',npolygons*4);
18data.Faces=data.Faces(2:4,:); %remove the leading number 3 - assuming mesh is trianglular
19data.Faces=data.Faces+1; %matlab complains otherwise
20
21% Search for Scalar data
22line=fsearch(fid,'CELL_DATA');
23ncells=sscanf(line,'CELL_DATA %d');
24class=fscanf(fid,'%s',1);
25var=fscanf(fid,'%s',1);
26type=fscanf(fid,'%s',1);
27line=fsearch(fid,'LOOKUP_TABLE');
28
29% Read in Scalar date
30data.FaceVertexCData=zeros(1,ncells);
31data.FaceVertexCData(:)=fscanf(fid,'%f',ncells);
32
33% Transpose things to order expected by patch command
34data.Faces=data.Faces';
35data.Vertices=data.Vertices';
36data.FaceVertexCData=data.FaceVertexCData';
37end
38
39function line=fsearch(fid,str)
40while true
41 line=fgetl(fid);
42 if numel(line) >= numel(str)
43 if (strcmp(line(1:numel(str)),str))
44 break
45 end
46 end
47end
48end