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!