How can I isolate and subplot June from the whole year?
4 views (last 30 days)
Show older comments
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
Accepted Answer
Torsten
on 21 Dec 2022
Edited: Torsten
on 21 Dec 2022
% 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
2 Comments
More Answers (1)
Eric Sofen
on 22 Dec 2022
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 Comments
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!