【各位观众老爷,父老乡亲们,给大家展示一下本人的劳苦
白天就给补上,四更!】
clc;
clear all;
%%注意变换参数,并做个性化标注,避免重复
file = 'sg625-20240119T0000.ncCF.nc3.nc'; % 替换为实际文件名
ncdisp(file);
% 读取数据
lon = ncread(file, 'longitude');
lat = ncread(file, 'latitude');
sali = ncread(file, 'salinity');
temp = ncread(file, 'temperature');
depth = ncread(file, 'depth');
time = ncread(file, 'time');
pres = ncread(file, 'pressure'); % 如果不需要,可以删除此行
% 获取唯一经纬度
lonc = unique(lon(:));
latc = unique(lat(:));
% 创建经纬度网格
[Lon, Lat] = meshgrid(lonc, latc);
% 找到每个唯一经度首次出现的位置索引
[~, idxLon] = ismember(lon(:), lonc, 'rows');
[~, firstIdx] = sortrows([idxLon, reshape(1:numel(lon), [], 1)]);
firstIdx = firstIdx(unique(idxLon));
% 提取对应的数据
tempc = temp(firstIdx);
depthc = depth(firstIdx);
salic = sali(firstIdx);
% 处理时间数据
timeBase = datenum('1970/01/01');
timeStr = datestr(time(firstIdx) / (24 * 3600) + timeBase, 'yyyy-mm-dd HH:MM:SS');
% 如果需要将时间字符串保存为数组,可以取消以下行的注释
% timeStrArray = str2cell(num2str(timeStr)); % 转换为cell数组以便保存
% 如果需要查看或保存处理后的数据,可以添加相关代码
% 例如,保存数据到新的.mat文件
% save('processedData.mat', 'Lon', 'Lat', 'tempc', 'depthc', 'salic', 'timeStr', 'ZI');
% 此代码运行前需要置入task2环境中(即2运行后才可运行)
ZI=griddata(lonc,latc,depthc,Lon,Lat);
figure
mesh(Lon,Lat,ZI);
set(gca,'zdir','reverse')
xlabel('Longitude')
ylabel('Latitude')
zlabel('Depth (m)')
figure
m_proj('Albers Equal-Area Conic','lon',[-100 -64],'lat',[0 40]);
m_gshhs_c('patch',[0.1 0.1 0.1]);
m_grid('box','fancy','xtick',[-100:3:-64],'ytick',[0:3:40],'tickdir','out','fontsize',12);
hold on
m_plot(lon,lat,'r')
figure
plot(lon,lat)
hold on
plot(lon(1,1),lat(1,1),'.','MarkerEdgeColor','k','MarkerSize',30);
plot(lon(537780,1),lat(537780,1),'.','MarkerEdgeColor','r','MarkerSize',30);
legend('路径','起点','终点')
axis equal
grid on
figure
plot(datenum(timec(:)/24/3600+datenum('1970/01/01')),depthc)
dateaxis('x',1,'19-Jan-2024')
set(gca,'ydir','reverse')
title('剖面深度变化')
ylabel('Depth (m)')
{下面的是正确代码}
clc;
clear all;
%%注意变换参数,并做个性化标注,避免重复
%%项目2:读取数据,分析数据特征
file='SG635-20170714T1601_731f_7d45_058f.nc';
ncdisp(file)
lon=ncread(file,'longitude');
lat=ncread(file,'latitude');
lonc=unique(lon);
latc=unique(lat);
sali=ncread(file,'salinity');
temp=ncread(file,'temperature');
den=ncread(file,'density');
depth=ncread(file,'depth');
time=ncread(file,'time');
[Lon,Lat]=meshgrid(lonc,latc);
pres=ncread(file,'pressure');
timec=unique(time);
k=1;
for i=1:537779;
if lon(i,1)==lon(i+1,1);
i=i+1;
else
j(k)=i;
k=k+1;
end
end
j(k)=537780;
for i=1:1499
tempc(i,1)=temp(j(i),1);
end
for i=1:1499
timec(i,1)=time(j(i),1);
end
for i=1:1499
depthc(i,1)=depth(j(i),1);
end
for i=1:1499
salic(i,1)=sali(j(i),1);
end
xztime=zeros(1499,1);
datestr(timec(:)/24/3600+datenum('1970/01/01'));
ZI=griddata(lonc,latc,depthc,Lon,Lat);
%%项目3:绘制轨迹图,注意标注坐标轴和其他说明文字,选择不同的地图投影类型,选择不同的线型和点型
% figure
% mesh(Lon,Lat,ZI);
% set(gca,'zdir','reverse')
% xlabel('Longitude')
% ylabel('Latitude')
% zlabel('Depth (m)')
%
% figure
% m_proj('Albers Equal-Area Conic','lon',[-100 -64],'lat',[0 40]);
% m_gshhs_c('patch',[0.1 0.1 0.1]);
% m_grid('box','fancy','xtick',[-100:3:-64],'ytick',[0:3:40],'tickdir','out','fontsize',12);
% hold on
% m_plot(lon,lat,'r')
%
% figure
% plot(lon,lat)
% hold on
% plot(lon(1,1),lat(1,1),'.','MarkerEdgeColor','k','MarkerSize',30);
% plot(lon(537780,1),lat(537780,1),'.','MarkerEdgeColor','r','MarkerSize',30);
% legend('路径','起点','终点')
% axis equal
% grid on
%
% figure
% plot(datenum(timec(:)/24/3600+datenum('1970/01/01')),depthc)
% dateaxis('x',1,'14-Jul-2017')
% set(gca,'ydir','reverse')
% title('剖面深度变化')
% ylabel('Depth (m)')
【写了一天的代码最后还报错了,心态炸了】
警告: Coastline file D:/fvcompost/m_map/private/gshhs_c.b not found
(Have you installed it? See the M_Map User's Guide for details)
---Using default coastline instead
> 位置:mu_coast>get_coasts (第 427 行)
位置: mu_coast (第 96 行)
位置: m_gshhs_c (第 35 行)
位置: task2 (第 57 行)
Index in position 1 exceeds array bounds. Index must not exceed 428.
出错 task2 (第 66 行)
plot(lon(537780,1),lat(537780,1),'.','MarkerEdgeColor','r','MarkerSize',30);