# PETSCVIEWERMATLAB
A viewer that saves the variables into a MATLAB .mat file that may be read into MATLAB with load('filename'). 


## Notes
Currently can only save PETSc vectors to .mat files, not matrices (use the `PETSCVIEWERBINARY` and
${PETSC_DIR}/share/petsc/matlab/PetscBinaryRead.m to read matrices into MATLAB).

For parallel vectors obtained with `DMCreateGlobalVector()` or `DMGetGlobalVector()` the vectors are saved to
the .mat file in natural ordering. You can use DMView() to save the `DMDA` information to the .mat file
the fields in the MATLAB loaded da variable give the array dimensions so you can reshape the MATLAB
vector to the same multidimensional shape as it had in PETSc for plotting etc. For example,

In your PETSc C/C++ code (assuming a two dimensional `DMDA` with one degree of freedom per node)
```none
                PetscObjectSetName((PetscObject)x,"x");
                VecView(x,PETSC_VIEWER_MATLAB_WORLD);
                PetscObjectSetName((PetscObject)da,"da");
                DMView(x,PETSC_VIEWER_MATLAB_WORLD);
```

Then from MATLAB
```none
                load('matlaboutput.mat')   % matlaboutput.mat is the default filename
                xnew = zeros(da.n,da.m);
                xnew(:) = x;    % reshape one dimensional vector back to two dimensions
```


If you wish to put the same variable into the .mat file several times you need to give it a new
name before each call to view.

Use `PetscViewerMatlabPutArray()` to just put an array of doubles into the .mat file


## See Also
 `PETSC_VIEWER_MATLAB_()`, `PETSC_VIEWER_MATLAB_SELF`, `PETSC_VIEWER_MATLAB_WORLD`, `PetscViewerCreate()`,
`PetscViewerMatlabOpen()`, `VecView()`, `DMView()`, `PetscViewerMatlabPutArray()`, `PETSCVIEWERBINARY`, `PETSCVIEWERASCII`, `PETSCVIEWERDRAW`,
`PETSC_VIEWER_STDOUT_()`, `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `PetscViewerFormat`, `PetscMatlabEngine`

## Level
intermediate

## Location
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/viewer/impls/matlab/vmatlab.c.html#PETSCVIEWERMATLAB">src/sys/classes/viewer/impls/matlab/vmatlab.c</A>


---
[Edit on GitLab](https://gitlab.com/petsc/petsc/-/edit/release/src/sys/classes/viewer/impls/matlab/vmatlab.c)


[Index of all Viewer routines](index.md)  
[Table of Contents for all manual pages](/docs/manualpages/index.md)  
[Index of all manual pages](/docs/manualpages/singleindex.md)  
