How can I isolate and subplot June from the whole year?

1 回表示 (過去 30 日間)
Hannah
Hannah 2022 年 12 月 21 日
回答済み: Eric Sofen 2022 年 12 月 22 日
I am needing to isolate and plot June 2014 from this data set. I have succesfully plotted the entire year on one tidal curve, but am needing to the same solely for the month of June. I have followed another question on here's advice but unfortunately all I am getting is a werid looking figure (I'll add this to a comment as it won't let me add to pictures to this question). Code below and the example of the entire year's tidal curve which is what I need June's one to also look like.
% Initialise
clear
close all
clc
% Load data
filename='DevonportTides2014.txt';
A=readtable(filename,'Headerlines',1);
data=table2array(A);
% Strip out data
Years=data(:,1);
Months=data(:,2);
Days=data(:,3);
Hours=data(:,4);
Minutes=data(:,5);
Seconds=data(:,6);
tide=data(:,7);
% Compute Matlab time
dnum=datenum(Years,Months,Days,Hours,Minutes,Seconds);
% Plot data
subplot(2,1,1) % (First subplot)
plot(dnum,tide,'k')
datetick
title('Devonport Tidal Height (2014)')
ylabel('(m rel. CD)')
xlabel('Month')
datetick
set(gca,'FontSize',12,'LineWidth',1.7)
grid on
hold on
% Subplot June 2014
juneRows=A.Month==6;
junetable=A(juneRows,:);
subplot(2,1,2)
title('Devonport Tidal Height (June 2014)')
ylabel('(m rel. CD)')
xlabel('Days')
set(gca,'FontSize',12,'LineWidth',1.7)
datetick
grid on
  1 件のコメント
Hannah
Hannah 2022 年 12 月 21 日
This is what I'm currently working with.

サインインしてコメントする。

採用された回答

Torsten
Torsten 2022 年 12 月 21 日
編集済み: Torsten 2022 年 12 月 21 日
% Initialise
clear
close all
clc
% Load data
filename='DevonportTides2014.txt';
A=readtable(filename,'Headerlines',1);
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
data=table2array(A);
% Strip out data
Years=data(:,1);
Months=data(:,2);
Days=data(:,3);
Hours=data(:,4);
Minutes=data(:,5);
Seconds=data(:,6);
tide=data(:,7);
% Compute Matlab time
dnum=datenum(Years,Months,Days,Hours,Minutes,Seconds);
% Plot data
subplot(2,1,1) % (First subplot)
plot(dnum,tide,'k')
datetick
title('Devonport Tidal Height (2014)')
ylabel('(m rel. CD)')
xlabel('Month')
datetick
set(gca,'FontSize',12,'LineWidth',1.7)
grid on
hold on
% Subplot June 2014
juneRows=data(:,2)==6;
datajune = data(juneRows,:);
dnumjune=datenum(datajune(:,1),datajune(:,2),datajune(:,3),datajune(:,4),datajune(:,5),datajune(:,6));
tidejune = tide(juneRows,:);
subplot(2,1,2)
plot(dnumjune,tidejune,'k')
title('Devonport Tidal Height (June 2014)')
ylabel('(m rel. CD)')
xlabel('Days')
set(gca,'FontSize',12,'LineWidth',1.7)
datetick
grid on
  2 件のコメント
Hannah
Hannah 2022 年 12 月 21 日
Thanks so much for getting back to me. I've tried this and I get this returned:
I'm wondering if I need to put something different in datajune = data(juneRows,:); and tidejune = tide(juneRows,:); as there's no numbers with the colons and commars?
Error in task2tidalcurve (line 35)
dnumjune=datenum(datajune(:,1),datajune(:,2),datajune(:,3),datajune(:,4),datajune(:,5),datajune(:,6));
Torsten
Torsten 2022 年 12 月 21 日
See above.

サインインしてコメントする。

その他の回答 (1 件)

Eric Sofen
Eric Sofen 2022 年 12 月 22 日
Using datenum is no longer recommended. I'd encourage you to use datetime for date and time calculations in MATLAB. Among other benefits (time zones, localization, precision...), many chart types offer automatic support for displaying datetimes on the axes, rather than needing to use datetick. Using table/timetable/datetime for this sort of work hopefully makes your code more expressive and, at least in this case, concise.
t = readtable("https://www.mathworks.com/matlabcentral/answers/uploaded_files/1239247/DevonportTides2014.txt",'Headerlines',1);
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
t.Time = datetime(t{:,1:6});
t(:,1:6) = [];
tt = table2timetable(t);
juneData = tt(timerange(datetime(2014,6,1),"month"),:)
juneData = 2880×1 timetable
Time Height__m_ ____________________ __________ 01-Jun-2014 00:00:00 1.92 01-Jun-2014 00:15:00 1.71 01-Jun-2014 00:30:00 1.54 01-Jun-2014 00:45:00 1.39 01-Jun-2014 01:00:00 1.26 01-Jun-2014 01:15:00 1.16 01-Jun-2014 01:30:00 1.11 01-Jun-2014 01:45:00 1.08 01-Jun-2014 02:00:00 1.1 01-Jun-2014 02:15:00 1.18 01-Jun-2014 02:30:00 1.31 01-Jun-2014 02:45:00 1.48 01-Jun-2014 03:00:00 1.69 01-Jun-2014 03:15:00 1.96 01-Jun-2014 03:30:00 2.25 01-Jun-2014 03:45:00 2.55
plot(juneData.Time,juneData.Height__m_, 'k')
title('Devonport Tidal Height (June 2014)')
ylabel('(m rel. CD)')
xlabel('Days')
set(gca,'FontSize',12,'LineWidth',1.7)

カテゴリ

Help Center および File ExchangeSubplots についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by