Image Example

Using images in matlab plots can offer some advantages as the pixel graphics is highly compressed. The axis elements are still vector elements. This combination is good for publications as font styles and labels can be reworked at any time.

For the following example we use topology data from the NASA Shuttle Radar Topography Mission (SRTM) project (http://eros.usgs.gov/products/elevation/). The height data is stored as signed 16bit integer as a 1201 × 1201 matrix. The topology height files can be found in http://dds.cr.usgs.gov/srtm/version2_1/SRTM3.

function demo_plot2svg_image
% Read in topology data
all_data = read_topo('N47E007.hgt');
figure
xax = linspace(7,8,size(all_data,1));
yax = linspace(47,48,size(all_data,2));
% Draw the image
imagesc(xax,yax,flipud(all_data));
% Change tickmarks to degree and minutes
set(gca,'YTick',[47:1/6:48]);
set(gca,'YTickLabel',convert_to_deg([47:1/6:48]));
set(gca,'XTick',[7:1/6:8]);
set(gca,'XTickLabel',convert_to_deg([7:1/6:8]));
set(gca,'YDir','normal');
grid on
box on
% Create some axis labels
xlabel('East');
ylabel('Nord');
title('Topography North of Switzerland')
% Force equal scaling of x and y
axis equal
axis tight
colorbar
% Call plot2svg to create the SVG plot
plot2svg('demo_image.svg')


function data = read_topo(name)
% Open the file in big-endian mode
fid = fopen([name '/' name],'r','b');
% Read signed 16bit data
data = fread(fid,1201*1201,'short=>short');
% Reformat to matrix
data = reshape(data,1201,1201)';
% Remove overlap
data = data(1:end - 1, 1:end - 1);
% Convert to double (most function can only handle double)
data = double(data);
% Remove missing data points by NaNs
data(data == -32768) = NaN;
fclose(fid);


function label = convert_to_deg(data)
deg = floor(data);
minutes = (data - deg) .* 60;
for i = 1:length(deg)
label{i} = [num2str(deg(i)) '°' num2str(minutes(i)) ''''];
end

Plot Result of the Image Example

The SVG plot includes two links to pixel graphics PNG files. The first one is the map. The second one is the color bar contents. The axis and labels are vector elements. Loading the plot into a SVG editor like e.g. Inkscape allows for modifications. Axis and figure contents are grouped. In order to modify single figure elements the groups have to be dissolved. The plot2svg function draws always a background to define the figure size. This is visible in the image below by the white background. In this case the background would not be needed. Loading the figure in Inkscape, ungrouping the elements, and removing the background is very simple.

Pixelgraphics of the SVG Plot.

View the SVG plot

Categories