How can I solve this datetick error?
7 views (last 30 days)
Show older comments
attatched file is sea.txt, it will work changing to sea.dat
I'm trying to plot this graph,
however, my graph plots like this.
I'm trying to convert julian date using datetick, but I have no clue what have I done wrong.
xlabel keep goes 01~01
I just need 2019 jan data, so I sliced like that.
clc
clear all
close all
fnm='sea.dat';
fid=fopen(fnm,'r');
data = textscan(fid, '%s%f%f%f%f%f%f%f', 'HeaderLines', 1, 'Delimiter',',');
all = [data{2:8}] ;
atemp = all(1:744,6);
apres = all(1:744,7);
date_str=data{1};
sample_date=date_str(1:744);
j_date=datenum(sample_date);
n_date=datevec(j_date);
x = j_date;
x1 = j_date(1:24:744);
figure
%%
y1 = atemp;
y2 = atemp(1:24:744);
subplot(2,2,[1 2])
plot(x,y1, 'k--')
dateformat = 7;
datetick('x', dateformat)
title('Weather at Observatory')
grid on
hold on
plot(x1,y2,'g*')
xlabel('Date (2019. Jan.)')
ylabel('Air temperature (℃)')
hold off
What have I missed in this case?
2 Comments
Dyuman Joshi
on 29 Nov 2023
Please share the data file you are working with (i.e. sea.dat). Use the paperclip button to attach.
Accepted Answer
Star Strider
on 29 Nov 2023
The datetick function uses MATLAB datenum values to calculate and plot the dates and times.
T1 = readtable('sea.txt', 'VariableNamingRule','preserve')
VN = T1.Properties.VariableNames;
Lv = (month(T1.time) == 1) & (year(T1.time)== 2019);
% LvHr1 = hour(T1.time) == 1;
Select = T1(Lv,:)
LvHr1 = hour(Select.time) == 1;
figure
plot(Select{:,1}, Select{:,7}, '--k')
hold on
plot(Select{LvHr1,1}, Select{LvHr1,7}, 'g*')
hold off
grid
xlabel(VN{1})
ylabel(VN{7})
.
5 Comments
Peter Perkins
on 29 Nov 2023
All of that is good stuff, but these data cry out for a timetable, i.e. the readtimetable function. Everything else stays more or less that same, but when you are done plotting against time, you may find that a timetable makes the other things you want to do easier.
T1 = readtimetable("https://www.mathworks.com/matlabcentral/answers/uploaded_files/1555502/sea.txt", VariableNamingRule="preserve")
More Answers (0)
See Also
Categories
Find more on Dates and Time 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!