FreeFem++ Data to Matlab

The article describe how to transfer FreeFem++ Data to Matlab

Matlab or FreeFem++, each tool has its advantage in computing of partial differentials equations. Sometimes you need a hybrid solution. For example you have a given variational formulation and you need a singular value decomposition of your solution. Using FreeFem++ to do the finite element part and doing the singular value decomposition by matlab would be a possibility. Consequently, you need a interface between Matlab and FreeFem++. A helpful tool is the File exchange Freefem2Matlab by Julien Dambrine which you can find on

You can use in Matlab the command system

('FreeFem++ yourFreefemProgramm.edp '); 

to execute the FreeFem++ file. Consequently, only one click is necessary for the whole computation. This avoids to do first the FreeFem computation in the Terminal (Linux) and than starting the routines in Matlab. Note that the Freefem programm must be executable and have to use the interface to transfer the data from Matlab2FreeFem, which we introduce below. Furthermore, you can execute every command you use to open a FreeFem++ file by system


In my opinion the FreeFem++MPI would also work but I did not tested it. Now, we have a look on the interface. First, you have to save your used FreeFem mesh (here denoted by Sh) in a file by


 Then in Matlab you can use importfilemesh.m of the "Toolbox" to import the mesh by


Second, you have to write the solutions computed in FreeFem++ into a file by ofstream file

ofstream file("u.val");
file << "2 1 1 "<< u[].n << " 2 \n";
for (int j=0;j<u[].n ; j++){
file << u[][j] << endl;

Here "u.val" denotes the filename and "u" is the solution computed in FreeFem. Next, we can import the solution in Matlab by 


To display now the mesh we use 

pdemesh(p,s,t) ;

and to display the solution in a 2D domain we use


and for the 3D domain using


For more information we refer the reader to the website of FreeFem++ Matlab