How can I solve this datetick error?
1 回表示 (過去 30 日間)
古いコメントを表示
attatched file is sea.txt, it will work changing to sea.dat
I'm trying to plot this graph,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1555472/image.png)
however, my graph plots like this.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1555477/image.png)
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!