Skip to content
Ricardo Torres edited this page Feb 16, 2022 · 1 revision

Karen Amoudry's Matlab plotting script

The question of how to plot unstructured data in Matlab has come up a few times. The script below is my solution to plotting FVCOM output, variables defined at both nodes and elements.

    % Basic recipe for plotting FVCOM output
    %
    % Karen Amoudry, NOC Liverpool
    
    % Where is the NetCDF file located?
    fname = 'FVCOM_OUTPUT.nc';     % YOUR FILE NAME HERE
    
    floc = '/FVCOM/output/location/';   % YOUR FILE LOCATION HERE
    
    %% Read in the NetCDF data
    lon=ncread([floc,fname],'x');   % longitude
    lat=ncread([floc,fname],'y');   % latitude
    zeta=ncread([floc,fname],'zeta'); % zeta
    tri=ncread([floc,fname],'nv');    % element-node index
    
    %% Plotting data defined at the nodes
    % Use trisurf if variable is defined at the nodes
    figure;trisurf(tri,lon,lat,zeta(:,end),'EdgeColor','none') % plot the trisurf
    % Options:  'FaceColor','flat' gives a blocky plot, one colour per triangle
    %           'FaceColor','interp' gives a smoother, interpolated plot
    
    %% Plotting data defined at the elements
    % Use patch if variable is defined at the element centre
    figure;patch('Faces',tri,'Vertices',[lon lat],'FaceColor','flat',...
         'FaceVertexCData',u(:,1,end),'CDataMapping','scaled','EdgeColor','none') % plot the patch
    
    %% Make the plot look nicer for easy viewing
    xlimits=get(gca,'XLim');    % get the x/y/z limits to move the camera
    ylimits=get(gca,'yLim');
    zlimits=get(gca,'zLim');
    xcam=((xlimits(2)-xlimits(1))./2)+xlimits(1);   % find the centre coords for the camera
    ycam=((ylimits(2)-ylimits(1))./2)+ylimits(1);
    zcam=((zlimits(2)-zlimits(1))./2)+zlimits(1);
    set(gca,'CameraPosition',[xcam,ycam,zlimits(2)],'CameraTarget',[xcam,ycam,zcam])
        % move the camera to the centre of the plot (effectively makes plot 2D)
    
    axis equal  % fix the axes
    xlim(xlimits)
    ylim(ylimits)
    zlim(zlimits)
    
    colorbar
Clone this wiki locally