MATLAB Answers

0

How can I plot hourly data in the same figure?

Asked by Suat YAZICI on 4 Apr 2019
Latest activity Commented on by dpb
on 7 Apr 2019
Accepted Answer by dpb
My data as follows:
  • 1st column: Year
  • 2nd column: Day of the year
  • 3st column: Hour
  • 6st column: Value
I'd like to do statistic analysis such as median, minimum, maximum, and mean for every season and then plot in the same figure. Problem is that hour column is not at regular intervals for me. That's why output figure is not what I want.

  2 Comments

dpb
on 5 Apr 2019
So what do you want?
Hour value is irregular. It changes its interval in related to years. For example, interval hour of 1st winter season is between 1 and 23 by increasing 2 but my code plots 0 value and there is no 0 value in this data.
I would like to do hourly variaton according to winter season in this data by calculating statistic analysis and then plotting it in the same figure according to its own hour values.

Sign in to comment.

1 Answers

Answer by dpb
on 5 Apr 2019
Edited by dpb
on 5 Apr 2019
 Accepted Answer

Use the features of Matlab to your advantage...granted, it takes some "time in grade!" to learn one's way around! :)
As a general rule, when one finds that are building specific indices into the data, that's a real sign there should be a better way...in this case, converting to datetime in order to do the comparisons is the trick. And, it turns out your Q? immediately follows essentially the same problem in financial analysis and is quite common also with such data as yours...the same 'trick" as used there (link) works very well here as well--
load winter.txt
w=table(datetime(winter(:,1),1,winter(:,2),winter(:,3),0,0),winter(:,6) ...
'VariableNames',{'Date','Data'}); % create table from array
w.WYear=fix((w.Date-w.Date(1))./duration(24*365+isleapyr(w.Date),0,0)); % compute the winter year
yr=unique(year(w.Date)); % retrieve the years covered
yrlabel=cellstr(num2str([yr mod(round(100*(yr/100-fix(yr/100)))+1,100)],'%d-%02d')); % build a label array
labels={'Winter','Mean','Median','Minimum','Maximum','StdDev','LowLimit','UpLimit'}; % output statistics
g=findgroups(w.WYear); % grouping variable by winter
[av,md,mn,mx,sd,lo,hi]=splitapply(@multipleStatistics,w.Data,g); % compute statistics
summ=table(categorical(yrlabel),av,md,mn,mx,sd,lo,hi,'VariableNames',labels); % build a summary table
>> summ =
summ =
20×8 table
Winter Mean Median Minimum Maximum StdDev LowLimit UpLimit
_______ ______ ______ _______ _______ ______ ________ _______
1998-99 17.909 14.9 4.1 51 10.114 4.3556 31.462
1999-00 21.358 18 3.4 63.2 13.36 3.4553 39.26
2000-01 22.821 19.75 5.3 60.7 13.627 4.5608 41.082
2001-02 38.467 34.45 5.3 92.5 20.885 10.481 66.453
2002-03 20.464 16.1 3.8 60.6 11.386 5.2074 35.721
2003-04 14.101 11.3 3.9 45.2 7.4069 4.1757 24.026
2004-05 11.731 10.1 0.2 31.2 5.4401 4.4414 19.021
2005-06 10.189 9.4 3.3 25.6 3.3511 5.6982 14.679
2006-07 9.063 8.1 0.7 19.8 3.1426 4.8519 13.274
2007-08 8.5001 8 3.2 22.2 2.6795 4.9095 12.091
2008-09 8.7789 8.6 2.8 17.9 2.3483 5.6322 11.926
2009-10 9.8379 9.5 2.8 25 3.2059 5.542 14.134
2010-11 10.533 10.05 3.5 35.4 3.7726 5.4773 15.588
2011-12 15.377 13.55 3.3 41.7 6.7471 6.3357 24.418
2012-13 13.316 11.5 3.2 41.1 5.8716 5.4482 21.184
2013-14 18.659 16.3 4 60.8 9.9698 5.2995 32.019
2014-15 20.451 17.1 5.1 50.9 10.52 6.3538 34.548
2015-16 12.464 10.7 3.3 34.8 5.6957 4.8319 20.096
2016-17 9.2583 8.5 3.6 23.9 3.185 4.9904 13.526
2017-18 8.8444 8.5 4.1 17 2.4073 5.6186 12.07
>>

  23 Comments

dpb
on 7 Apr 2019
That's one way to solve the appearance issue, yes, but... I was trying to coax you to figure out what is actually going on inside the square brackets as a teaching opportunity... so you'd have another piece of Matlab syntax/idiomatic usage in your toolbox going forward. :)
Follow along the following sequence of operations at the command line -- something along these lines is what I was suggesting you do to understand...
>> yr=1999;
>> yrlabel=cellstr(num2str([yr mod(round(100*(yr/100-fix(yr/100)))+1,100)],'%d-%02d'))
>> (num2str([yr mod(round(100*(yr/100-fix(yr/100)))+1,100)],'%d-%02d'))
>> [yr mod(round(100*(yr/100-fix(yr/100)))+1,100)]
>> [yr]
>> [mod(round(100*(yr/100-fix(yr/100)))+1,100)]
>> (round(100*(yr/100-fix(yr/100)))+1)
>> (yr/100-fix(yr/100))
>> (num2str([yr],'%d'))
>> yrlabel=cellstr(num2str(yr,'%d'))
>> yr=unique(year(w.Date(1)));
>> yrlabel=cellstr(num2str(yr,'%d'))
that progressively goes into the innards piecewise to tear it down to its component parts, then gradually builds it back up again with the desired result instead...this is a key technique in picking up other peoples' code to figure out what it is doing when not completely apparent. With, of course diligent study of the documentation of any functions used like mod and num2str here.
Thank you so much for patient, kind and explanation.
dpb
on 7 Apr 2019
'tis OK, I enjoy interaction with those who are trying...and am known for prodding sometimes instead of just answering straight out to hopefully end up with more learning in the process...

Sign in to comment.



Translated by