Matlab/Octave scripts

From GHER

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

Contents

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://modb.oce.ulg.ac.be/home/svn/repos/Scripts/trunk Scripts

A snapshot of 09/05/2014 can be downloaded as zip file for those not familiar with SVN here

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

IO
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' )

Normally you will just use gread.m and gwrite.m in your matlab/codes codes


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:

addpath(genpath('/path/to/Scripts'))

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

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.

pcolor(x,y,z);
shading flat % or shading interp

contourf

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:

contourf(X,Y,Z,100,'edgecolor','none');

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

scatter

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
scatter(xx,yy,50,zz,'filled','s')
  • 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');
colormap(interp1(x,jet,y));

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
pcolor(lon,lat,sst)
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
contourf(lon,lat,sst,100,'edgecolor','none');

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
scatter(lon2,lat2,2,sst,'filled','s')

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.

savesas(gcf,'sst2010','fig')

After that, the figure can be loaded again using

open('sst2010.fig')

Export in usual image formats

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

print('-dpng','-r300','diagram1')

where

  • -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

print('-depsc','-r300','diagram1')

it is recommended to employ the set of functions exportfig.m, previewfig.m, applytofig.m and restorefig.m, available at http://www.mathworks.com/company/newsletters/digest/december00/export.html

exportfig(gcf,'gher.eps','bounds','tight','Color','rgb','Resolution',300); 

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

print('-dpdf','-r300','diagram1')

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

http://www.myoutsourcedbrain.com/2009/07/produce-print-quality-figures-from.html

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'
/home/charles/.matlab/R2008b/history.m

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

Personal tools