matlab example
Written by Daniel Herber on January 4, 2016.
Combining these 2 MATLAB Central submissions results in a powerful piece of code for exporting your high quality figures to specific sensible locations.
---
### Default Behavior for mfoldername and export_fig
> Exports figures nicely to a number of vector & bitmap formats - export_fig
[export_fig](http://www.mathworks.com/matlabcentral/fileexchange/23629) uses a delimited string for `varargin`. For example, `export_fig 'myfigure' -pdf -png` outputs a pdf and png file in the current directory with the name
> Creates a path to easily save or load your results no matter where your function is located - mfoldername
[mfoldername](http://www.mathworks.com/matlabcentral/fileexchange/40397) requires 2 inputs and outputs the folder name of the specified function. The first input is the function path (either dynamic using `mfilename('fullpath')` or static using the name of a function in your current path. The second input is the additional directory that you would like to add to the path. For example, `mypath = mfoldername(mfilename('fullpath'),'')` will output the folder name for the function the it is run in since the second argument is an empty string.
---
### Combining Them
First we need to add both `mfoldername` and all the contents of `export_fig` to your MATLAB path. One easy way to do this is placing `mfoldername` in the same folder as your function and using it to add all items from the folder and subfolders automatically to your path.
```matlab
% generate string of the folder name for this file
mypath1 = mfoldername(mfilename('fullpath'),'');
addpath(genpath(mypath1)) % add contents
```
Now we would like to create a figure utilizing the peaks function. The `export_fig` function outputs everything in the figure as it appears. Since the default figure background color is grey, we will need to change the color to white for a consistent looking figure.
```matlab
figure('Color',[1 1 1]); % change the background to white
peaks
```
Finally we are at the point of saving the figure. We need to provide the figure name, folder name, and `export_fig` options. The folder name can be empty if you want to save the figure in the same folder as the calling function. Since `export_fig` uses a variable length input, we will need to create a complete string with all the options specified and the correct save location. Once this string has been created, we can use `eval` to evaluate the string.
```matlab
figname = 'peaks'; % name the figure
foldername = 'figs'; % name the folder the figure will be placed in
exportfigopts = '-pdf -png'; % export_fig options (see documentation)
mypath2 = mfoldername(mfilename('fullpath'),foldername); % folder string
filename = [mypath2,figname]; % combine folder string and name string
str = ['export_fig ''',filename,''' ',exportfigopts]; % total str for export_fig
eval(str) % evaluate and save the figure
```
Based on the options passed to `export_fig` a pdf and png image of the figure is generated using the default options. The these files were placed in a specific folder in relation to the function. If your m file was called myfunction.m, then the resulting file system would look like the following:
```markdown
/
/myfunction.m
/mfoldername.m
/figs/
/figs/peaks.pdf
/figs/peaks.png
/exportfig/
```
PDF output file:
PNG output file:
[](blogs/matlab/post_3/peaks.png){data-lightbox="blog_imgs" data-title="PNG output file"}
---
### Complete Example File with Minimal Comments
```matlab
% myfunction.m
% add all contents at and below this file to your path
mypath1 = mfoldername(mfilename('fullpath'),'');
addpath(genpath(mypath1))
% create the figure
figure('Color',[1 1 1]); % change the background to white
peaks
% save current figure with export_fig
figname = 'peaks';
foldername = 'figs';
exportfigopts = '-pdf -png';
mypath2 = mfoldername(mfilename('fullpath'),foldername);
str = ['export_fig ''',[mypath2,figname],''' -pdf -png'];
eval(str)
```