存一下代码
clear;
clc;
ncfile = 'woa18_decav_s00_04.nc';
varname = 's_an';
lonRange = [105 136];
latRange = [ 3 45];
target_lon = 135;
target_lat = 20;
lon = ncread( ncfile, 'lon' ); % longitude
lat = ncread( ncfile, 'lat' ); % latitude
depth = ncread( ncfile, 'depth' ); % depth
tg = ncread( ncfile, 'time' ); % time
% (a) for longitude, find the nearest zonal location
i_inx = find( lon >= lonRange(1) & ...
lon <= lonRange(end) );
% (b) for latitude
[tmp, j_inx] = min( abs(lat - target_lat) );
% (c) for depth, take all depth levels
z_inx = 1:length(depth);
nx = length( i_inx ); % longitude
ny = length( j_inx ); % laitude
nz = length( z_inx ); % depth
xx = lon(i_inx);
yy = depth;
% Define start vector (where to start your read/write)
start = [i_inx(1) j_inx(1) z_inx(1) 1];
% Define count vector (how many points to read)
count = [nx ny nz 1];
% Read data
salt = ncread( ncfile , ... % Source, i.e. hycom.nc in this case
varname, ... % Varialbe, "salt" in this case
start , ... % The Index of Start Point along each Dimension
count ); % How many points to read along each Dimension
salt = squeeze(salt)';
% plot
pcolor( xx, yy, salt );
shading flat;
colormap( jet );
caxis([33.5 35.5]); colorbar
set( gca, 'FontSize' , 18 , ...
'FontName' , 'Helvetica', ...
'TickDir' , 'Out' , ...
'xMinorTick', 'On' , ...
'yMinorTick', 'On' , ...
'TickLength', [0.02 0.03], ...
'Box' , 'On' , ...
'Layer' , 'Top' , ...
'yDir' , 'reverse' );
title( sprintf('%s (%d %s N)', 'salinity', target_lat, char(176)), ...
'FontSize', 20 , ...
'FontName', 'HelVetica' );
xlabel( 'Longitude (\circE)', ...
'FontSize', 20 , ...
'FontName', 'HelVetica');
ylabel( 'Depth (m)' , ...
'FontSize', 20 , ...
'FontName', 'HelVetica');