MATLAB Examples

Example

Contents

Introduction

The example I use here is inspired from
http://rucool.marine.rutgers.edu/manuals/tag/pcolor/.
First, we generate coordinates as a [3000 x 1] vectors. If we have to
create a pseudocolor plot of the corresponding grid, this is goind to be
time consumming, because the matrix will be [3000 x 3000]. By using the
function scatter2Pcolor and a limited resolution for the grid, we speed
up the computation time, and still have a good result.
clear all;close all;clc;
% generate coordinates
x = rand(3000,1)*200-100;  %Lon coordinates
y = rand(3000,1)*200-100;  %Lat coordinates
z = 10*sin((x*pi/180).^2+(y*pi/180).^3)+20;  %Faux-measured data

Minimal code given by rucool.marine.rutgers.edu :

tic
[X,Y] = meshgrid(unique(x),unique(y));
Z = griddata(x,y,z,X,Y);
figure
pcolor(X,Y,Z); shading flat; colorbar;
hold on; plot(x,y,'k.')  %Original data points;
toc
Elapsed time is 14.445450 seconds.

Application of function scatter2Pcolor without options

tic
figure
scatter2Pcolor(x,y,z)
toc
Elapsed time is 0.263232 seconds.

Application of function scatter2Pcolor with multiple options

tic
figure
scatter2Pcolor(x,y,z,'display','yes','resolution',200,'Xlabel',...
    'my x label','Zlabel','my Z label','Ylabel','my Y label');
toc
Elapsed time is 0.222991 seconds.

Case with nan or inf values for the coordinates x and y

y(1:10:end)=nan;
x(5:10:end)=inf;
tic
figure
scatter2Pcolor(x,y,z,'display','yes','resolution',200,'Xlabel',...
    'my x label','Zlabel','my Z label','Ylabel','my Y label');
toc
Warning: The coordinates x of the input points contains Inf values. 
Warning: The coordinates y of the input points contains NaN values. 
Elapsed time is 0.217517 seconds.

Conclusions

The function scatter2Pcolor is found to be particularly useful when:
  • the set of data is heterogeneously distributed in the grid
  • The size of the data set is large, leading to a high computational cost, if pcolor is directly used.