Matlab/Octave scripts


Revision as of 12:10, 9 May 2014 by Jmb (Talk | contribs)
Jump to: navigation, search


Download GHER scripts

To download the scripts, you need an account on modb. On a Linux/Unix computer, the scripts can be downloaded with the svn command:

svn checkout svn+ssh:// Scripts

some instructions from a Windows user?

The scripts are organized in subdirectories. Currently, it only contains:

Input-output functions for the GHER fileformat (in big-endian; if you experience problems reading or writing GHER binary files, you might need to swith to little endian by editing uwrite.m and change line format = 'ieee-be' into format = 'ieee-le'

You can include the directory Scripts (with all subdirectories) in your search path by adding the following to your startup.m (matlab) or ~/.octaverc (octave) file:


More information about SVN:

Creation of figures

2D field plots

Here we examine the different possibilities to create a colored map. The examples can be tried with articial data:

[x,y,z] = peaks(100);


pcolor stands for Pseudocolor (checkerboard) plot. The color of a given cell is specified by the corresponding value of the matrix. The shading controls the color shading.

shading flat % or shading interp


contourf fills the areas between contours with colors according to the Z-value. It is possible to have a result very similar to pcolor by specifying a large number of contours to be plotted:


means that 100 contour levels are required, and that the contours themselves are not represented (by default, a black line separate the different levels).


A scatter plot displays colored circles (or other shapes) at the locations specified by the vectors X and Y. If can also be applied to simulate the bahaviour of pcolor.

% Put the matrix into vector form
xx = X(:); yy = Y(:); zz = Z(:);
% Make the plot
  • 50 is the size of the circles (or other forms) that will be plotted
  • 'filled' means that the circles will be colored (otherwise only their contour is shown, the center is white)
  • 's' means that we want squares instead of circles

Non-linear color maps

Sometimes it is necessary to zoom the color map on a certain region on values. If the values are very close to zero (and still positive), one can plot the logarithm. If this is not the case, one can use a color map transformed with a non-linear functions:

x = linspace(0,1,64);
y = interp1([0 .5 1],[0 .9 1],x,'pship');

The more the .9 is different from .5, the more the transformation would be non-linear.

Incomplete satellite images

This application is a particular case of the 2D plots:

  • data are geo-referenced, hence it is recommended to use the m_map package.
  • holes or missing data may exist.

The first step is to replace the missing values (for example, -999) by NaN, in order to ensure that the place without measurement will remain blank.

valex = -999;
sst(sst==valex) = NaN;

The second step is to make the plot. We can use one of the three possibilities from the previous section, each of them having its advantages and inconvients.

  • pcolor
shading flat 

When the number of data points to represent is too large, the exportation of the figure in postscript may give unsatisfactory results: the image is in raster, instead of being in vectorial format. This can be seen by looking at the axes label, or at the title. In some cases, the degree symbol ° disappears from the figure. The resulting eps file has a size of 15M.

  • contourf

Here the quality of the results will depend on the kind of data and on the percentage of missing data. In the bad cases, the plot can take a very long time to be finished.

  • scatter
% change matrices into vectors
lon = lon(:); lat = lat(:); sst = sst(:) 
% remove missing values
lon(isnan(sst)) = [];
lat(isnan(sst)) = [];
sst(isnan(sst)) = [];
% change coordinates into x,y (m_scatter does not exist!)
[lon2,lat2] = m_ll2xy(lon,lat);
% make the plot

Exporting figures

Each of the following sections presents a different way to export or save a graphic created using matlab.

Save as matlab .fig file

A matlab figure can be saved as a .fig file (matlab format) using the command saveas.


After that, the figure can be loaded again using


Export in usual image formats

The figure can also be exported in various image formats. This is done using the command print.



  • -dpng specifies the output format.
  • -r300 specifies the resolution in dpi.
  • diagram1 is the name of the newly created file. Note that the extension is automatically appended.

Export in eps

When you want to export figures as Encapsuled PostScript (eps), instead of using


it is recommended to employ the set of functions exportfig.m, previewfig.m, applytofig.m and restorefig.m, available at


The results will have a better quality than the one obtained with print.

Export in pdf

When you want to export figures as pdf, instead of using


it is recommended to export the figure in eps, as explained in the previous paragraph, and then convert it in pdf, using for instance:

ps2pdf diagram1.eps

Export in svg

SVG stands for Scalable Vector Graphics. To export in this format with matlab, download the package plot2svg

Miscellaneous Tips

  • Forget to save a .m file?

There is a file called history.m, which contents the last commands you type.

[charles@gher13 figures]$ find /home/charles/ -name 'history.m'

This way you may be able to rebuild the content of the unsaved file.

Personal tools