-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsave_displacementVTK_binary.m
38 lines (29 loc) · 1.37 KB
/
save_displacementVTK_binary.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function save_displacementVTK_binary(u,v,w,x,y,z,filename)
nr_of_elements=numel(x);
fid = fopen(filename, 'w');
%ASCII file header
fprintf(fid, '# vtk DataFile Version 3.0\n');
fprintf(fid, 'VTK from Matlab\n');
fprintf(fid, 'BINARY\n\n');
fprintf(fid, 'DATASET STRUCTURED_GRID\n');
fprintf(fid, ['DIMENSIONS ' num2str(size(x,1)) ' ' num2str(size(x,2)) ' ' num2str(size(x,3)) '\n']);
fprintf(fid, ['POINTS ' num2str(nr_of_elements) ' float\n']);
fclose(fid);
%append binary x,y,z data
fid = fopen(filename, 'a');
fwrite(fid, [reshape(x,1,nr_of_elements); reshape(y,1,nr_of_elements); reshape(z,1,nr_of_elements)],'float','b');
%append another ASCII sub header
fprintf(fid, ['\nPOINT_DATA ' num2str(nr_of_elements) '\n']);
fprintf(fid, 'VECTORS displacement_vectors float\n');
%append binary u,v,w data
fwrite(fid, [reshape(u,1,nr_of_elements); reshape(v,1,nr_of_elements); reshape(w,1,nr_of_elements)],'float','b');
%append some scalar data
fprintf(fid, '\nSCALARS DisplacementMagnitude float\n'); %ASCII header
fprintf(fid, 'LOOKUP_TABLE default\n'); %ASCII header
fwrite (fid, reshape(sqrt(u.^2+v.^2+w.^2),1,nr_of_elements),'float','b'); %binary data
% %append some scalar data
% fprintf(fid, '\nSCALARS Pressure float\n'); %ASCII header
% fprintf(fid, 'LOOKUP_TABLE default\n'); %ASCII header
% fwrite (fid, reshape(pressure,1,nr_of_elements),'float','b'); %binary data
fclose(fid);
return