Borrar filtros
Borrar filtros

How can I solve this datetick error?

2 visualizaciones (últimos 30 días)
kim
kim el 29 de Nov. de 2023
Comentada: Peter Perkins el 29 de Nov. de 2023
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 comentarios
Dyuman Joshi
Dyuman Joshi el 29 de Nov. de 2023
Please share the data file you are working with (i.e. sea.dat). Use the paperclip button to attach.
kim
kim el 29 de Nov. de 2023
I fogot about that. I just attached it.

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 29 de Nov. de 2023
The datetick function uses MATLAB datenum values to calculate and plot the dates and times.
A much simpler approach uis to use the datetime function and the readtable function—
T1 = readtable('sea.txt', 'VariableNamingRule','preserve')
T1 = 8760×8 table
time sea level(cm) wtemp(℃) salinity(PSU) wspd(m/s) wdir(deg) atemp(℃) apres(hPa) ____________________ _____________ ________ _____________ _________ _________ ________ __________ 01-Jan-2019 00:00:00 36.88 7.81 33.54 3.84 256.42 0.13 1030.1 01-Jan-2019 01:00:00 37.93 7.8 33.5 4.98 240.65 -0.5 1029.8 01-Jan-2019 02:00:00 35.83 7.64 33.57 5.64 258.28 -1.14 1029.3 01-Jan-2019 03:00:00 34.95 7.52 33.5 2.81 262.18 -1.37 1029.4 01-Jan-2019 04:00:00 31.73 7.41 33.52 6.4 271.75 -1.7 1028.9 01-Jan-2019 05:00:00 30.92 7.4 33.47 6.51 266.83 -1.86 1028.2 01-Jan-2019 06:00:00 30.33 7.35 33.54 5.84 269.12 -1.81 1028.7 01-Jan-2019 07:00:00 34.8 7.4 33.52 5.58 275.83 -1.67 1028.7 01-Jan-2019 08:00:00 30.87 7.4 33.59 7.05 278.87 -1.64 1029 01-Jan-2019 09:00:00 33.88 7.4 33.5 6.51 272.57 -1.29 1028.5 01-Jan-2019 10:00:00 36.23 7.4 33.56 6.68 262.85 -0.03 1028.2 01-Jan-2019 11:00:00 38.2 7.51 33.56 6.68 272.07 0.88 1027.3 01-Jan-2019 12:00:00 40.22 7.6 33.54 7.08 269.05 1.15 1026.5 01-Jan-2019 13:00:00 41.57 7.64 33.61 8.22 277.1 1.87 1025.3 01-Jan-2019 14:00:00 39.55 7.74 33.56 7.95 279.97 2.42 1025.4 01-Jan-2019 15:00:00 39.82 7.81 33.55 6.88 294.68 2.62 1025.8
VN = T1.Properties.VariableNames;
Lv = (month(T1.time) == 1) & (year(T1.time)== 2019);
% LvHr1 = hour(T1.time) == 1;
Select = T1(Lv,:)
Select = 744×8 table
time sea level(cm) wtemp(℃) salinity(PSU) wspd(m/s) wdir(deg) atemp(℃) apres(hPa) ____________________ _____________ ________ _____________ _________ _________ ________ __________ 01-Jan-2019 00:00:00 36.88 7.81 33.54 3.84 256.42 0.13 1030.1 01-Jan-2019 01:00:00 37.93 7.8 33.5 4.98 240.65 -0.5 1029.8 01-Jan-2019 02:00:00 35.83 7.64 33.57 5.64 258.28 -1.14 1029.3 01-Jan-2019 03:00:00 34.95 7.52 33.5 2.81 262.18 -1.37 1029.4 01-Jan-2019 04:00:00 31.73 7.41 33.52 6.4 271.75 -1.7 1028.9 01-Jan-2019 05:00:00 30.92 7.4 33.47 6.51 266.83 -1.86 1028.2 01-Jan-2019 06:00:00 30.33 7.35 33.54 5.84 269.12 -1.81 1028.7 01-Jan-2019 07:00:00 34.8 7.4 33.52 5.58 275.83 -1.67 1028.7 01-Jan-2019 08:00:00 30.87 7.4 33.59 7.05 278.87 -1.64 1029 01-Jan-2019 09:00:00 33.88 7.4 33.5 6.51 272.57 -1.29 1028.5 01-Jan-2019 10:00:00 36.23 7.4 33.56 6.68 262.85 -0.03 1028.2 01-Jan-2019 11:00:00 38.2 7.51 33.56 6.68 272.07 0.88 1027.3 01-Jan-2019 12:00:00 40.22 7.6 33.54 7.08 269.05 1.15 1026.5 01-Jan-2019 13:00:00 41.57 7.64 33.61 8.22 277.1 1.87 1025.3 01-Jan-2019 14:00:00 39.55 7.74 33.56 7.95 279.97 2.42 1025.4 01-Jan-2019 15:00:00 39.82 7.81 33.55 6.88 294.68 2.62 1025.8
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 comentarios
Star Strider
Star Strider el 29 de Nov. de 2023
As always, my pleasure!
Peter Perkins
Peter Perkins el 29 de Nov. de 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")
T1 = 8760×7 timetable
time sea level(cm) wtemp(℃) salinity(PSU) wspd(m/s) wdir(deg) atemp(℃) apres(hPa) ____________________ _____________ ________ _____________ _________ _________ ________ __________ 01-Jan-2019 00:00:00 36.88 7.81 33.54 3.84 256.42 0.13 1030.1 01-Jan-2019 01:00:00 37.93 7.8 33.5 4.98 240.65 -0.5 1029.8 01-Jan-2019 02:00:00 35.83 7.64 33.57 5.64 258.28 -1.14 1029.3 01-Jan-2019 03:00:00 34.95 7.52 33.5 2.81 262.18 -1.37 1029.4 01-Jan-2019 04:00:00 31.73 7.41 33.52 6.4 271.75 -1.7 1028.9 01-Jan-2019 05:00:00 30.92 7.4 33.47 6.51 266.83 -1.86 1028.2 01-Jan-2019 06:00:00 30.33 7.35 33.54 5.84 269.12 -1.81 1028.7 01-Jan-2019 07:00:00 34.8 7.4 33.52 5.58 275.83 -1.67 1028.7 01-Jan-2019 08:00:00 30.87 7.4 33.59 7.05 278.87 -1.64 1029 01-Jan-2019 09:00:00 33.88 7.4 33.5 6.51 272.57 -1.29 1028.5 01-Jan-2019 10:00:00 36.23 7.4 33.56 6.68 262.85 -0.03 1028.2 01-Jan-2019 11:00:00 38.2 7.51 33.56 6.68 272.07 0.88 1027.3 01-Jan-2019 12:00:00 40.22 7.6 33.54 7.08 269.05 1.15 1026.5 01-Jan-2019 13:00:00 41.57 7.64 33.61 8.22 277.1 1.87 1025.3 01-Jan-2019 14:00:00 39.55 7.74 33.56 7.95 279.97 2.42 1025.4 01-Jan-2019 15:00:00 39.82 7.81 33.55 6.88 294.68 2.62 1025.8

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by