# How to get a matrix into Matlab

**URL:** <https://community.freefem.org/t/how-to-get-a-matrix-into-matlab/620>\
**Category:** General Discussion\
**Created:** [October 20, 2020, 12:41am UTC](https://community.freefem.org/t/how-to-get-a-matrix-into-matlab/620 "2020-10-20T00:41:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dajopez](https://avatars.discourse-cdn.com/v4/letter/d/a87d85/32.png) [@dajopez](https://community.freefem.org/u/dajopez)\
**Post date:** [October 20, 2020, 12:41am UTC](https://community.freefem.org/t/how-to-get-a-matrix-into-matlab/620/1 "2020-10-20T00:41:11Z")

</div>

Hello,

I have a bunch of matrices calculated for a specific problem and want to put them into Matlab for data analysis. I can already see them in the log file, but I want the individual matrices saved as their own files so I can mess around with them individually.

thanks for the help

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [October 20, 2020, 9:04am UTC](https://community.freefem.org/t/how-to-get-a-matrix-into-matlab/620/2 "2020-10-20T09:04:58Z")

</div>

```auto
$ cat matlab.edp
real[int, int] A(2, 2);
A = 1;
matrix sp = A;
load "PETSc"
Mat B(sp); // use your "matrix" here, sp is just a placeholder
ObjectView(B, format = "matlab", name = "first.m");
sp = 4 * sp;
Mat C(sp); // use another "matrix" here, sp is just a placeholder
ObjectView(C, format = "matlab", name = "second.m");
$ ff-mpirun -n 1 matlab.edp -v 0 && cat first.m && cat second.m
[...]
%Mat Object: 1 MPI processes
% type: mpiaij
% Size = 2 2
% Nonzeros = 4
zzz = zeros(4,3);
zzz = [
1 1 1.0000000000000000e+00
1 2 1.0000000000000000e+00
2 1 1.0000000000000000e+00
2 2 1.0000000000000000e+00
];
 Mat_0x84000000_0 = spconvert(zzz);
%Mat Object: 1 MPI processes
% type: mpiaij
% Size = 2 2
% Nonzeros = 4
zzz = zeros(4,3);
zzz = [
1 1 4.0000000000000000e+00
1 2 4.0000000000000000e+00
2 1 4.0000000000000000e+00
2 2 4.0000000000000000e+00
];
 Mat_0x84000000_1 = spconvert(zzz);

```

---

<div class="post-metadata">

**Author:** ![D\_N\_Sarkar](https://avatars.discourse-cdn.com/v4/letter/d/0ea827/32.png) [@D\_N\_Sarkar](https://community.freefem.org/u/D_N_Sarkar)\
**Post date:** [October 20, 2020, 12:19pm UTC](https://community.freefem.org/t/how-to-get-a-matrix-into-matlab/620/3 "2020-10-20T12:19:34Z")

</div>

(1) Use ‘ofstream’ to write the FreeFEM matrices as text files. Then read the text file in MATLAB to form the matrix using the row and column index and the corresponding value.

(2) [https://github.com/samplemaker/freefem\_matlab\_octave\_plot](https://github.com/samplemaker/freefem_matlab_octave_plot)

(3) [https://www.mathworks.com/matlabcentral/fileexchange/26833-freefem-to-matlab](https://www.mathworks.com/matlabcentral/fileexchange/26833-freefem-to-matlab)

(4) [https://www.um.es/freefem/ff++/uploads/Main/FFmatrix\_fread.m](https://www.um.es/freefem/ff++/uploads/Main/FFmatrix_fread.m)
