How can I isolate and subplot June from the whole year?
조회 수: 3 (최근 30일)
이전 댓글 표시
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
채택된 답변
Torsten
2022년 12월 21일
편집: Torsten
2022년 12월 21일
% 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=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
추가 답변 (1개)
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);
t.Time = datetime(t{:,1:6});
t(:,1:6) = [];
tt = table2timetable(t);
juneData = tt(timerange(datetime(2014,6,1),"month"),:)
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)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!