Data Export from Femlab Structure to Matlab
- Some examples for postprocessing in Matlab -
1. Introduction
Femlab uses the Matlab script language with additional Femlab specific
commands.
But you cannot call Femlab 3.1 at the command line and execute such a
script file. Therefore, it is necessary to use Matlab to control
Femlab. If you do this you have the chance to use the results
of the calculation stored in the fem-structure
for postprocessing in Matlab. The aim of this tutorial is to show you
how to control Femlab with Matlab and how to export the results from
the fem-structure into Matlab.
2. Using Matlab to control Femlab
Detailed information about the fem-structure and the Femlab-/Matlab
commands used in this tutorial can be found in the help desks of Femlab and
Matlab. To demonstrate the solution of the asked
problem a simple plate capacitor model solving the poisson equation is used.
To start a Femlab script file with Matlab, enter at the command line
femlab matlab path -ml -nodisplay -mlr myfemlabscript
Notice that the typical file type of the script file is "m". But you have to specify the script file without the file type. The filename must not start with a number. If you want to suppress all graphical outputs use the option
'Report','off'
for the meshinit and solution function.
3. Data export of stationary solutions
3.1. 1D
The most simple case is a time-independent solution in only one
spatial dimension (script file).
At first you have to define an array of spatial
coordinates. At these points you will export your solution.
dim=100; x=linspace(0,1,dim);
The variable dim defines how many values you want to export in the range [0:1]. The data between these mesh points will be interpolated. To export the data of the potential V you can call the Femlab function postinterp()
mat_result=postinterp(fem,'V',x);
The variable mat_result is a vector of the dimension dim. If you remove the variable fem and save the file in Matlab, you do not need Femlab anymore. To plot these simple data in Matlab, call
plot(x,mat_result);
The complete export script can be found here.
3.2. 2D
Use this script file to create 2D data sets.
In two dimensions it is a little bit more complicate to export the required
data. At first you have to create arrays of the spatial coordinates, too.
dim=100; x=linspace(0,1,dim); y=linspace(0,1,dim);
To get and plot the data values at the diagonal spatial coordinates (xi,yi), use the commands
mat_result_diagonal=postinterp(fem,'V',[x;y]); plot3(x,y,mat_result_digonal);
As a rule you want to export the data of the complete geometry. Therefore, you have to prepare a mesh which defines the coordinates for the export
dim=100;
x=linspace(0,1,dim);
y=linspace(0,1,dim);
[x,y]=meshgrid(x,y);
mygrid=[x(:),y(:)]';
mat_result_vector=postinterp(fem,'V',mygrid);
To plot these data in Matlab, call
plot3(mygrid(1,1:dim*dim),mygrid(2,1:dim*dim),mat_result_vector);
If you want to plot a surface using the function surf(), you will get an error message. The reason for this is the structure of the array mat_result_vector. It looks like
| 1 | 2 | ... | dim | dim+1 | ... | 2*dim | 2*dim+1 | ... | dim*dim |
|---|---|---|---|---|---|---|---|---|---|
| x1,y1 | x1,y2 | ... | x1,ydim | x2,y1 | ... | x2,ydim | x3,y1 | ... | xdim,ydim |
The function surf() expects both the spatial coordinates and the data in the form of an array data_array(x,y). To realize this you have two possibilities: You can use a loop over the geometry and call the function postinterp() at everyone of these points. But this is not recommended because it is not very efficient. Therefore, it is much more easy to convert the existing array mat_result_vector like
mat_result_matrix=reshape(mat_result_vector,dim,dim); surf(x,y,mat_result_matrix);
Download the complete export script here.
4. Data export of time-dependent solutions
In the case of time-dependent solutions you have to export the
results at every time step.
4.1. 1D
The corresponding script file for data creation
can be found here.
At first you have to define an export mesh, too, followed by the export and
postprocessing commands
dim=100;
x=linspace(0,1,dim);
[t,mat_result]=postinterp(fem,'t','V',x,'solnum',1:length(fem.sol.tlist));
plot(x,mat_result(1,1:dim));
The rows in the mat_result array comply with the solutions at the given time steps saved in the vector t (export script).
4.2. 2D
This case is a little bit more interessant because an inhomogeneity was
added to the problem (script file).
In this section it will be demontrated how to create a movie file
with Matlab after the successfull export of the Femlab data.
The procedure is the same like in the other cases:
- define coordinate arrays
- set export grid
- export data (at every time step)
- transform export array for usage with function surf()
- create movie
Therefore, use the following commands
dim=100; x=linspace(0,1,dim); y=linspace(0,1,dim); [x,y]=meshgrid(x,y); mygrid=[x(:),y(:)]'; [t,mat_result_vector]=postinterp(fem,'t','V',mygrid, 'solnum',1:length(fem.sol.tlist)); plot3(mygrid(1,1:dim*dim),mygrid(2,1:dim*dim),mat_result_vector(1,1:dim*dim))
Remember the structure of the result vector explained above in the time-independent case. But now this vector is a matrix because every row complys a single time step solution. To use the function surf(), the matrix has to be transformed like in the following example for the time step number 20
mytimestep=20; tmp=reshape(mat_result_vector(mytimestep,:),dim,dim); surf(x,y,tmp);
Get the complete Matlab script (data export, data conversion, figure and movie creation)
here.
Finally, some results are shown:

- Video sequence (DivX encoded)