How can I solve this datetick error?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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.
kim
2023년 11월 29일
I fogot about that. I just attached it.
채택된 답변
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')
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
kim
2023년 11월 29일
Thanks for helping. However, I got a little problem.
Actually, my file is on other language and I can't change the language of heatherlines by the way.
Because of this reason, I can't use your code
Lv = (month(T1.time) == 1) & (year(T1.time)== 2019);
is there any other way with using datetick?
You can calculate ‘Lv’ using column references (note the use of curly braces {}) rather than variable names:
Lv = (month(data{:,1}) == 1) & (year(data{:,1}) == 2019);
Also, you can rrefer to the variablle names by reference to the ‘VN’ cell array, as I do here.
data = readtable('sea.txt', 'VariableNamingRule','preserve')
data = 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 = data.Properties.VariableNames;
Lv = (month(data{:,1}) == 1) & (year(data{:,1}) == 2019);
% LvHr1 = hour(data{:,1}) == 1;
Select = data(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{:,1}) == 1;
figure
plot(Select{:,1}, Select{:,7}, '--k', 'DisplayName',VN{7})
hold on
plot(Select{LvHr1,1}, Select{LvHr1,7}, 'g*', 'DisplayName',[string(VN{7})+" 01:00"])
hold off
grid
xlabel(VN{1})
ylabel(VN{7})
legend('Location','best')

.
kim
2023년 11월 29일
Solved it.
Thanks for your help.
Star Strider
2023년 11월 29일
As always, my pleasure!
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
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
