Draw temperature vertical distribution, Plot error
    5 ビュー (過去 30 日間)
  
       古いコメントを表示
    
To see upwelling, I would like to draw the vertical distribution of the temperature for August. If there is no data for August, I would like to draw it using the data for September. There is data for September 2006, but it says there is no data. Please help me
%% 초기화
clear all; close all; clc;
data = readtable('data_line_102.xlsx', 'VariableNamingRule', 'preserve');
date = data{:, 7};  % 날짜 데이터
temp = data{:, 10}; % 수온 데이터
sail = data{:, 11}; % 수심 데이터
dep = data{:, 9};
lon = data{:, 6};
lat = data{:, 5};
line = data{:, 2};
% 날짜 데이터를 datetime 형식으로 변환
date = datetime(date, 'InputFormat', 'yyyy-MM-dd HH:mm');
%% 8월 또는 9월 데이터로 연직 분포 생성
years = 1993:2020; 
august_month = 8;  % 8월 우선
september_month = 9;  % 9월 대체
num_years = length(years);
subplot_cols = 5;  % 한 행에 5개 플롯 설정
num_rows = ceil(num_years / subplot_cols);  % 필요한 행 수 계산
for row = 1:num_rows
    f = figure;
    t = tiledlayout(1, subplot_cols, 'TileSpacing', 'Compact', 'Padding', 'Compact');
    for col = 1:subplot_cols
        year_idx = (row - 1) * subplot_cols + col;
        if year_idx > num_years
            break;
        end
        current_year = years(year_idx);
        % 8월 데이터를 우선 필터링
        filtered_data = data(year(date) == current_year & month(date) == august_month, :);
        % 8월 데이터가 없으면 9월 데이터를 필터링
        if isempty(filtered_data)
            filtered_data = data(year(date) == current_year & month(date) == september_month, :);
        end
        % 8월과 9월 데이터가 모두 없으면 건너뜁니다.
        if isempty(filtered_data)
            continue;
        end
        % 필요한 데이터 추출
        temp_month = filtered_data{:, 10};  % 수온 데이터
        depth_month = filtered_data{:, 9};  % 수심 데이터
        lon_month = filtered_data{:, 6};    % 경도 데이터
        [lon_grid, depth_grid] = meshgrid(unique(lon_month), unique(depth_month));
        if numel(lon_month) < 2 || numel(depth_month) < 2
            warning('데이터 포인트가 충분하지 않습니다. %d년 8월(또는 9월)을 건너뜁니다.', current_year);
            continue;
        end
        temp_grid = griddata(lon_month, depth_month, temp_month, lon_grid, depth_grid, 'linear');
        if all(isnan(temp_grid(:)))
            warning('보간된 온도 그리드가 NaN 값만 포함하고 있습니다. %d년 8월(또는 9월)을 건너뜁니다.', current_year);
            continue;
        end
        ax = nexttile;
        contourf(lon_grid, depth_grid, temp_grid, 15, 'LineColor', 'none');
        xlabel('경도 (°)');
        if col == 1
            ylabel('수심 (m)');
        else
            ax.YTickLabel = [];
        end
        xlim([129.588, 130.918]);
        ylim([0, 100]);
        title(sprintf('%d년', current_year));
        set(gca, 'YDir', 'reverse');
        colormap(jet(15));
        clim([5 20]);
    end
    cb = colorbar;
    cb.Layout.Tile = 'east';
    cb.Label.String = '온도 (°C)';
    filename = sprintf('E:\\동해냉수대\\정선 8월 연직구조\\August_or_September_Temperature_Row_%d.png', row);
    saveas(f, filename);
    close(f);
end
0 件のコメント
採用された回答
  Taylor
    
 2024 年 11 月 13 日
        Seems to work fine for me. 2006 does have data for both August and September, but all of the August data is a single longitude value so I just hardcoded it to look for the September data in 2006. 2013 has no data for Agusut or September.
%% Set up the Import Options and import the data
opts = spreadsheetImportOptions("NumVariables", 11);
% Specify sheet and range
opts.Sheet = "Sheet1";
opts.DataRange = "A2:K4824";
% Specify column names and types
opts.VariableNames = ["Var1", "line", "Var3", "Var4", "lat", "lon", "date", "Var8", "depth", "temp", "sail"];
opts.SelectedVariableNames = ["line", "lat", "lon", "date", "depth", "temp", "sail"];
opts.VariableTypes = ["char", "double", "char", "char", "double", "double", "string", "char", "double", "double", "double"];
% Specify variable properties
opts = setvaropts(opts, ["Var1", "Var3", "Var4", "date", "Var8"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Var1", "Var3", "Var4", "date", "Var8"], "EmptyFieldRule", "auto");
% Import the data
data = readtable("data_line_102.xlsx", opts, "UseExcel", false);
data = sortrows(data,"date","ascend");
data.date = datetime(data.date, "InputFormat", "yyyy-MM-dd HH:mm");
%% Clear temporary variables
clear opts
years = unique(year(data.date));
augMon = 8;
sepMon = 9;
f = figure;
t = tiledlayout("flow", "TileSpacing", "Compact", "Padding", "Compact");
for ii = 1:length(years)
    clear ax filteredData long_grid depth_grid temp_grid augIdx sepIdx
    augIdx = month(data.date) == augMon & year(data.date) == years(ii);
    sepIdx = month(data.date) == sepMon & year(data.date) == years(ii);
    if years(ii) == 2006
        filteredData = data(sepIdx,:);
    elseif any(augIdx)
        filteredData = data(augIdx,:);  
    else
        filteredData = data(sepIdx,:);
    end
    ax = nexttile;
    [lon_grid, depth_grid] = meshgrid(unique(filteredData.lon), unique(filteredData.depth));
    temp_grid = griddata(filteredData.lon, filteredData.depth, filteredData.temp, lon_grid, depth_grid, 'linear');
    contourf(lon_grid, depth_grid, temp_grid, 15, 'LineColor', 'none');
    title(string(years(ii)))
end
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


