## Matlab/Octave code to compute centroid of 2d triangular element

It’s a simple code that I use. Sometimes I need to compute the centroid position of a finite element 2D triangle. You can download the code here:

## Saving scalar finite element data from Matlab to VTK

I found this link to save scalar and vector data from Matlab to unstructured grid in VTK legacy format:
http://people.sc.fsu.edu/~burkardt/m_src/twod_to_vtk/twod_to_vtk.html

However, I need only the scalar part, so I modified accordingly. You can download my code here:

## Plotting irregular finite element data in matlab/octave

I am solving a 2D finite element problem and I got x,y,z vectors. The length of these vectors are the number of nodes in my problem. To use the function “mesh”, we need to have x and y coming from “meshgrid”. To plot these data, use the following:
``` [xi,yi]=meshgrid(x,y); zi=griddata(x,y,z,xi,yi); mesh(xi,yi,zi); ```
Note that xi,yi, and zi are now a matrix.

## Matlab data save to VTK format

I was looking for some function to save matlab array to VTK data format, and found just what I wanted in this page. They are shown below with some modification on my own:

Scalar data:

```function savevtk(array, filename)
%  savevtk Save a 3-D scalar array in VTK format.
%  savevtk(array, filename) saves a 3-D array of any size to
%  filename in VTK format.
[nx, ny, nz] = size(array);
fid = fopen(filename, 'wt');
fprintf(fid, '# vtk DataFile Version 2.0\n');
fprintf(fid, 'Comment goes here\n');
fprintf(fid, 'ASCII\n');
fprintf(fid, '\n');
fprintf(fid, 'DATASET STRUCTURED_POINTS\n');
fprintf(fid, 'DIMENSIONS    %d   %d   %d\n', nx, ny, nz);
fprintf(fid, '\n');
fprintf(fid, 'ORIGIN    0.000   0.000   0.000\n');
fprintf(fid, 'SPACING    1.000   1.000   1.000\n');
fprintf(fid, '\n');
fprintf(fid, 'POINT_DATA   %d\n', nx*ny*nz);
fprintf(fid, 'SCALARS scalars double\n');
fprintf(fid, 'LOOKUP_TABLE default\n');
fprintf(fid, '\n');
for a=1:nz
for b=1:ny
for c=1:nx
fprintf(fid, '%d ', array(c,b,a));
end
fprintf(fid, '\n');
end
end
fclose(fid);
return
```

Vector data:

```function savevtkvector(X, Y, Z, filename)
%  savevtkvector Save a 3-D vector array in VTK format
%  savevtkvector(X,Y,Z,filename) saves a 3-D vector of any size to
%  filename in VTK format. X, Y and Z should be arrays of the same
%  size, each storing speeds in the a single Cartesian directions.
if ((size(X) ~= size(Y)) | (size(X) ~= size(Z)))
fprint('Error: velocity arrays of unequal size\n'); return;
end
[nx, ny, nz] = size(X);
fid = fopen(filename, 'wt');
fprintf(fid, '# vtk DataFile Version 2.0\n');
fprintf(fid, 'Comment goes here\n');
fprintf(fid, 'ASCII\n');
fprintf(fid, '\n');
fprintf(fid, 'DATASET STRUCTURED_POINTS\n');
fprintf(fid, 'DIMENSIONS    %d   %d   %d\n', nx, ny, nz);
fprintf(fid, '\n');
fprintf(fid, 'ORIGIN    0.000   0.000   0.000\n');
fprintf(fid, 'SPACING    1.000   1.000   1.000\n');
fprintf(fid, '\n');
fprintf(fid, 'POINT_DATA   %d\n', nx*ny*nz);
fprintf(fid, 'VECTORS vectors double\n');
fprintf(fid, '\n');
for a=1:nz
for b=1:ny
for c=1:nx
fprintf(fid, '%f ', X(c,b,a));
fprintf(fid, '%f ', Y(c,b,a));
fprintf(fid, '%f ', Z(c,b,a));
end
fprintf(fid, '\n');
end
end
fclose(fid);
return
```

## How to check Matlab License

This is the command to check Matlab license in my server:

`% cd /apps/matlab/r2006b/etc`
The folder might be different from yours. But you need to go to the /etc folder and then run lmstat -a to show all info.
`% ./lmstat -a`

Type ./lmstat –help if you want to use other options