Matlab_08

Simple data visualisation

There is a Matlab data file attached to this page which contains a 100-by-100 matrix called m_08. The exercises refer to this file but it could just as easily be a file of your own. Download the .mat file, store it in your working directory, then load it;

>> clear all; load matlab_08;

Our first job is to view all the data in visual form. We have a huge number of simple options in Matlab. You should try all of the following.

plot(m_08);
surf(m_08)
pcolor(m_08);
pcolor(m_08); shading interp;
imagesc(m_08)
contour(m_08);
contour(m_08,40);

Lets have a look at just one or two of these options in more detail. Start a new script (as in Matlab_06) but this time we are going to turn it in to a function. Functions are like scripts but cannot (usually) be run from within the editor. They must be called by name from the command line. This is the first time we have done this. After you pasted the code in to the editor and saved it (do not change the name suggested by Matlab, is has to be the same as the function name) you can run it from the command line like this:

>> return_value = my_graph(m_08)

Be careful, the function must be saved with a file-name that is the same as the function name

function return_value = my_graph(any_data)
%
%
% ---
% return_value = my_graph(any_data)
%
%   a function that takes a matrix
%   and displays it in various ways
%
%   any_data is a matrix
%   return_value is zero if the plot is successful
% ---
    return_value = 0;
    h1 = subplot(2,2,1);
    h2 = subplot(2,2,2);
    h3 = subplot(2,2,[3 4]);
    try
        subplot(h1);
        imagesc(any_data);
        colorbar;
        axis xy;
    catch
        return_value = return_value+1;
    end
    try
        subplot(h2);
        contour(any_data, 20);
        colorbar
    catch
        return_value = return_value+2;
    end
    try
        subplot(h3);
        sh = surf(any_data);
        set(sh, 'edgecolor', 'interp', 'linestyle', 'none');
        view(-83,22)
        grid on;
    catch
        return_value = return_value+4;
    end
end