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

조회 수: 3 (최근 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

채택된 답변

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 CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by