# How to read complex matrix in matlab

**URL:** <https://community.freefem.org/t/how-to-read-complex-matrix-in-matlab/3089>\
**Category:** General Discussion\
**Created:** [April 8, 2024, 11:39am UTC](https://community.freefem.org/t/how-to-read-complex-matrix-in-matlab/3089 "2024-04-08T11:39:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amitkaush](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/amitkaush/32/1978_2.png) [@Amitkaush](https://community.freefem.org/u/Amitkaush)\
**Post date:** [April 8, 2024, 11:39am UTC](https://community.freefem.org/t/how-to-read-complex-matrix-in-matlab/3089/1 "2024-04-08T11:39:41Z")

</div>

I am writing a complex matrix using ofstream in FreeFEM++

```auto
`ofstream file("A.dat");
           file << A <<endl;

```

And I am trying to read it in MATLAB using dlmread but it causes error.  
Can someone suggest a better way to write the complex matrix and read it in MATLAB

---

<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:** [April 9, 2024, 5:01am UTC](https://community.freefem.org/t/how-to-read-complex-matrix-in-matlab/3089/2 "2024-04-09T05:01:33Z")

</div>

```auto
load "PETSc-complex"
Mat<complex> dump(A);
ObjectView(dump, format = "matlab", name = "A.dat");

```

---

<div class="post-metadata">

**Author:** ![Amitkaush](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/amitkaush/32/1978_2.png) [@Amitkaush](https://community.freefem.org/u/Amitkaush)\
**Post date:** [April 9, 2024, 5:53am UTC](https://community.freefem.org/t/how-to-read-complex-matrix-in-matlab/3089/3 "2024-04-09T05:53:12Z")

</div>

Can you tell how to read this file in MATLAB ?  
thanks

---

<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:** [April 9, 2024, 6:51am UTC](https://community.freefem.org/t/how-to-read-complex-matrix-in-matlab/3089/4 "2024-04-09T06:51:29Z")

</div>

Sorry, the file extension should be .m, then simply type the file name (without the extension) in MATLAB.

---

<div class="post-metadata">

**Author:** ![Amitkaush](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/amitkaush/32/1978_2.png) [@Amitkaush](https://community.freefem.org/u/Amitkaush)\
**Post date:** [April 9, 2024, 7:20am UTC](https://community.freefem.org/t/how-to-read-complex-matrix-in-matlab/3089/5 "2024-04-09T07:20:51Z")

</div>

Earlier I used this method to save the matrix, but for my current case when i try to run the file in MATLAB i get the message " File is too large for MATLAB to run"

---

<div class="post-metadata">

**Author:** ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)\
**Post date:** [April 9, 2024, 11:10am UTC](https://community.freefem.org/t/how-to-read-complex-matrix-in-matlab/3089/6 "2024-04-09T11:10:01Z")

</div>

That’s a matlab issue, not a freefem one. If you’re using this for eigenvalue calculations, you should just use SLEPc directly.

---

<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:** [April 9, 2024, 4:21pm UTC](https://community.freefem.org/t/how-to-read-complex-matrix-in-matlab/3089/7 "2024-04-09T16:21:32Z")

</div>

What Chris says. The likelihood that MATLAB can do something that PETSc or SLEPc can’t is very slim. But if you insist on reading large files in MATLAB (which itself is a rather bad idea), you could do the following.

```auto
load "PETSc-complex"
Mat<complex> dump(A);
ObjectView(dump, format = "binary", name = "A.dat");

```

In MATLAB, use [share/petsc/matlab/PetscBinaryRead.m · main · PETSc / petsc · GitLab](https://gitlab.com/petsc/petsc/-/blob/main/share/petsc/matlab/PetscBinaryRead.m) and do:

```auto
A = PetscBinaryRead('A.dat', 'complex', true);

```
