How can I solve this datetick error?
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
Dyuman Joshi
2023-11-29
Please share the data file you are working with (i.e. sea.dat). Use the paperclip button to attach.
采纳的回答
Star Strider
2023-11-29
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 个评论
Peter Perkins
2023-11-29
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")
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!