How to plot observations against season and time of day?

Hello every one,
DayMonth = datetime('12/06/2004');
T = {'19:04 - 19:51:02'};
%T = {'10:10 - 10:30:02'};
%DayMonth = datetime('15/06/2004');
%T = {'03:20 - 03:45:06'};
%DayMonth = datetime('15/07/2005');
%T = {'07:10 - 07:50:07'};
% split to start and end times
startEndTimes = cellfun(@(str) strsplit(str,' - '),T,'UniformOutput',0);
startTimes = cellfun(@(c) c{1},startEndTimes,'UniformOutput',0);
endTimes = cellfun(@(c) c{2},startEndTimes,'UniformOutput',0);
% % add seconds where missing
missingSecondsIdx = cellfun(@length,startTimes) == 5;
startTimes(missingSecondsIdx) = cellfun(@(str) [str ':00'],startTimes(missingSecondsIdx),'UniformOutput',0);
missingSecondsIdx = cellfun(@length,endTimes) == 5;
endTimes(missingSecondsIdx) = cellfun(@(str) [str ':00'],endTimes(missingSecondsIdx),'UniformOutput',0);
% % convert time strings to numbers
startTimeNums = datenum(startTimes,'HH:MM:SS');
EPS = 1e-4;
endTimeNums = datenum(endTimes,'HH:MM:SS') - EPS;
contTime = linspace(startTimeNums(1),endTimeNums(end),200);
repDayMonth = repmat(DayMonth,[2 1]);
repDayMonth = repDayMonth(:);
allTimes = [startTimeNums';endTimeNums'];
allTimes = allTimes(:);
contDayMonth = interp1(allTimes,repDayMonth,contTime);
p=plot([DayMonth(1),DayMonth(1)],[startTimeNums(1),endTimeNums(1)],'b-','Markersize',5,'LineWidth',1);
datetick('x','dd/mm')
datetick('y','HH:MM')
This code gives me the plot given below
Now the plot gives me the correct information's (time and day of month). Now the question is that how to plot the other observations on the same figure?. I tried hold on, but i did not work. I want to get the plot as:
Any help will be appreciated.
Thanks

6 comentarios

dpb
dpb el 23 de Mzo. de 2018
No data...nothing can do without to see what you're working from in c
abdur rauf
abdur rauf el 23 de Mzo. de 2018
Editada: abdur rauf el 29 de Mzo. de 2018
Dear dpb, Actually i have observations of physical phenomenon for which the day of month and time is given
DayMonth = day('12/06/2004','dd/mm/yyyy');
T = {'19:04 - 19:51:02'};
I want to plot to plot this observation time (T) against time (UT) and season (e.g. 12 June). Thanks
dpb
dpb el 24 de Mzo. de 2018
DATA!!!!???? Can't reproduce anything w/o something to start from and I'm not going to try to reproduce what you've already got, sorry...
dpb
dpb el 24 de Mzo. de 2018
Editada: dpb el 31 de Mzo. de 2018
That is, you say "having PMSE observation time T, and the corresponding date."
Where are they?; they're not in the script are they? If so, I can't decipher what is what without more comments/explanation of "who's who in the zoo" than is in the code.
As to "how"; I'd have to think about that some with some data in hand; I think possibly simply line will do it with the start/stop time for each contiguous times inserting a NaN to break the lines between each separate period.
But, that's the information one needs to build the plot that isn't here...
I'd guess the way to get the data into Matlab would be to just create a text file to read in that contains each observation period start:stop time stamp either as two columns or paired times in one column; whether all years are in one file or there's a separate one for each year or whatever is pretty-much up to whatever is most convenient to prepare the data given its source, but that's what you need in some form or fashion however you choose to do so.
abdur rauf
abdur rauf el 25 de Mzo. de 2018
Editada: Walter Roberson el 25 de Mzo. de 2018
@dpb,
thank you sir so much for spending your precious time.
I do not claim that what i have done for getting the plot is correct. This is just to show what i have tried at least.
My PMSE observations time for different years are given below.
12 July 2004 T= 09:02:52 - 09:52:01
13 July 2004 T= 08:00:04 - 08:57:23
T= 09:26:53 - 09:28:18
14 July 2004 T= 09:34:04 - 09:35:18
09 July 2005 T= 08:04:08 - 08:05:54
T= 09:41:52 - 09:43:06
12 July 2007 T= 09:15:00 - 09:27:01
16 July 2007 T= 07:30:58 - 07:31:56
06 July 2008 T= 14:42:42 - 14:46:58
07 June 2010 T= 11:27:12 - 11:33:24
05 July 2011 T= 11:04:50 - 11:07:28
11 July 2011 T= 09:15:40 - 09:19:26
21 July 2011 T= 07:35:02 - 07:54:00
02 July 2012 T= 10:12:14 - 10:21:07
21 June 2013 T= 09:15:26 - 09:16:43
22 June 2013 T= 09:57:07 - 09:58:57
T= 10:41:16 - 10:59:55
T= 11:21:02 - 11:57:55
T= 12:09:31 - 12:55:45
23 June 2013 T= 10:06:00 - 10:20:00
T= 12:32:24 - 12:48:48
26 July 2013 T= 11:40:28 - 11:53:45
17 July 2015 T= 05:48:04 - 05:59:12
T= 06:00:14 - 06:29:55
T= 07:17:21 - 07:50:00
I wish you ignore what i have done to get the required plot. You do it your own way to get a plot showing my PMSE observation time on the corresponding day of month and year.
Thanks
dpb
dpb el 25 de Mzo. de 2018
Well, to date (so to speak :) ) that code is all there's been to look at so what else were we to do?
This is a start; we do finally have the datapoints of interest in front of us. Now where's your code that reads those data and tries to create the plot from them? As recommended earlier, I'd suggest to start by normalizing that set of data so can read it conveniently into an array of dates/times (having missing dates in first column can be dealt with, but makes the reading more complicated than just duplicating the date for no more than there are).
Then, insert a NaN between each element in the time vector of start:stop times to stop the line (that's how PLOT() works in Matlab; when it sees NaN in a sequence of points it lifts the pen until the next non-NaN element is reached). If you use a fairly wide line width and format the time on 24hr clock, I'm guessing you'll get a reasonable approximation of the paper's figure.
If you use the builtin datetime object, it will interpret the times axes on its own albeit you may need to set the limits to make presentation look desirable.
The X vector will be the date of the observation for each time period while the Y vector will be the start/stop times. Note you'll have to duplicate the "double-up" the days to match the length of the Y vector.
With so many of your observation times being so short, your plot isn't going to be nearly as solid as the Figure; many of those are just barely going to even show up -- 1 minute out of a day is only 1/1440th of the length of the y axis; don't be surprised if it doesn't show up at all unless you make the marker larger.
At least give it a go on your own; don't expect somebody else to solve your problem for you entirely...

Iniciar sesión para comentar.

 Respuesta aceptada

dpb
dpb el 28 de Mzo. de 2018
Editada: dpb el 28 de Mzo. de 2018
Well, here's a (not quite so) crude second pass...
Incorporated comments from last night -- good luck! :)
timedata=readtable('abdur.dat');
refdate=clock; % use current year for the reference
yr=refdate(1); mo=refdate(2); da=refdate(3);
dates=datetime(timedata.Date,'InputFormat','ddMMMMyyyy','Format','ddMMMM'); % dates, without year
timedata.Start=datetime(timedata.Start,'InputFormat','HH:mm:ss','Format','HH:mm:ss'); % start time; will be relative today's date
timedata.Stop=datetime(timedata.Stop,'InputFormat','HH:mm:ss','Format','HH:mm:ss'); % stop time...
timedata.Date=datetime(strcat(cellstr(dates),num2str(yr)),'InputFormat','ddMMMMyyyy');
% now we've got the necessary data into a table; need to construct line segments to plot
% start by getting unique years and separate list by year
[u,ia]=unique(year(dates)); % unique years, location in dates array first of each year
ib=cumsum([1;3*diff(ia)]); % location in full X,Y vector of start,stop,NaT sets of 3
% each record in start/stop fields is one line segment; introduce NaT to break lines
X=[timedata.Date.'; timedata.Date.'; NaT(1,height(timedata))]; X=X(:);
Y=[timedata.Start.'; timedata.Stop.'; NaT(1,height(timedata))]; Y=Y(:);
H=datenum(Y); H=24*(H-fix(H)); % convert to 0:24 hours for plotting
figure
ib=[ib;length(X)]; % augment index array with final index
plot(X(ib(1):ib(2)),H(ib(1):ib(2)),'linewidth',3) % first year segment
hold on % so can add on
for i=2:length(ib)-1 % and the rest...
plot(X(ib(i):ib(i+1)),H(ib(i):ib(i+1)),'linewidth',3)
end
% dress up plot a little and annotate; much room here for creativity... :)
xlim([datetime(yr,5,1) datetime(yr,8,1)])
legend(num2str(u),'location','southwest')
ylabel('UTC')
This places a line at the correct position on the axes; the width of that line is arbitrary so doesn't actually reflect the time taken by the observation relative to the axis scale.
The "exercise for the student" to do that if this for a published paper or thesis or dissertation would be to convert from using PLOT() and the line to PATCH() or FILL() such that the area actually is representative of the length of a day on the x-axis. That may have the effect of making the data almost invisible, however owing to the extremely short time periods of many of the observations so you may be forced to using a much shorter axis range or other technique to actually be able to visualize your real data as compared to that in the reference paper that had much more observation time.

12 comentarios

@ Dear dpb,
Which of my file you called 'abdur.dat'?
Thanks
dpb
dpb el 29 de Mzo. de 2018
Editada: dpb el 29 de Mzo. de 2018
The one I had pasted in with the first cut that I made from the data you posted "cleaned up" as I suggested some time ago....what it is is up to you however you want to get the data in but as noted, why not make it easier instead of harder to read?
The file I made here looked/looks like
>> type abdur.dat
Date Start Stop
12July2004 09:02:52 09:52:01
13July2004 08:00:04 08:57:23
13July2004 09:26:53 09:28:18
14July2004 09:34:04 09:35:18
13July2004 08:04:08 08:05:54
...
You can format however you want; I just simplified up front since it's such a short file doesn't take but a minute or two...
@ Dear dpb,
I am really sorry that I do not understand something supposed to be very easy. what i posted last time was
Datetime_str=string({'12-July-2004 09:02:52 09:52:01';'13-July-2004 08:00:04 08:57:23';'13-July-2004 09:26:53 09:28:18';'14-July-2004 09:34:04 09:35:18';'09-July-2005 08:04:08 08:05:54';'09-July-2005 09:41:52 09:43:06';'12-July-2007 09:15:00 09:27:01';'16-July-2007 07:30:58 07:31:56';'06-July-2008 14:42:42 14:46:58';'07-June-2010 11:27:12 11:33:24';'05-July-2011 11:04:50 11:07:28';'11-July-2011 09:15:40 09:19:26';'21-July-2011 07:35:02 07:54:00';'02-July-2012 10:12:14 10:21:07';'21-June-2013 09:15:26 09:16:43';'22-June-2013 09:57:07 09:58:57';'22-June-2013 10:41:16 10:59:55';'22-June-2013 11:21:02 11:57:55';'22-June-2013 12:09:31 12:55:45';'23-June-2013 10:06:00 10:20:00';'23-June-2013 12:32:24 12:48:48';'26-July-2013 11:40:28 11:53:45';'17-July-2015 05:48:04 05:59:12';'17-July-2015 06:00:14 06:29:55';'17-July-2015 07:17:21 07:50:00'})
and now you have
abdur.dat
Date Start Stop
12July2004 09:02:52 09:52:01
13July2004 08:00:04 08:57:23
13July2004 09:26:53 09:28:18
14July2004 09:34:04 09:35:18
13July2004 08:04:08 08:05:54
I do not know how to get abdur.dat from my last posted data.
Thanks.
dpb
dpb el 29 de Mzo. de 2018
Editada: dpb el 29 de Mzo. de 2018
Actually what you posted way back at <Comment_549407> looked like
12 July 2004 T= 09:02:52 - 09:52:01
13 July 2004 T= 08:00:04 - 08:57:23
T= 09:26:53 - 09:28:18
14 July 2004 T= 09:34:04 - 09:35:18
...
with the missing date fields and the extraneous text on the times that I kept telling you makes for more difficult input than would be if were more simple.
I just cut 'n pasted that text into the editor, named the file abdur.dat and manually turned it into a much more-easily-scanned format without the missing dates and eliminated the spurious text. Nothing magic here...
ADDENDUM
Actually, to make a file from your string is pretty simply that way without manual editing, too...
>>fid=fopen('abdur.txt','wt'); % get valid file handle to create a file
>> fprintf(fid,'%s\t%s\t%s\n','Date','Start','Stop'); % write a header
>> fprintf(fid,'%s\n',Datetime_str); % write the data
>> fid=fclose(fid); % close the file
>> type abdur.txt % see what we put into the file
Date Start Stop
12-July-2004 09:02:52 09:52:01
13-July-2004 08:00:04 08:57:23
13-July-2004 09:26:53 09:28:18
14-July-2004 09:34:04 09:35:18
09-July-2005 08:04:08 08:05:54
09-July-2005 09:41:52 09:43:06
12-July-2007 09:15:00 09:27:01
16-July-2007 07:30:58 07:31:56
06-July-2008 14:42:42 14:46:58
07-June-2010 11:27:12 11:33:24
05-July-2011 11:04:50 11:07:28
11-July-2011 09:15:40 09:19:26
21-July-2011 07:35:02 07:54:00
02-July-2012 10:12:14 10:21:07
21-June-2013 09:15:26 09:16:43
22-June-2013 09:57:07 09:58:57
22-June-2013 10:41:16 10:59:55
22-June-2013 11:21:02 11:57:55
22-June-2013 12:09:31 12:55:45
23-June-2013 10:06:00 10:20:00
23-June-2013 12:32:24 12:48:48
26-July-2013 11:40:28 11:53:45
17-July-2015 05:48:04 05:59:12
17-July-2015 06:00:14 06:29:55
17-July-2015 07:17:21 07:50:00
>>
That's slightly different format but doesn't have the missing days and can be parsed with just slightly different format string; the hyphens ("-") in the dates like the colons in the times aren't that bad; it was the other stuff that wasn't symmetric but primarily the missing dates that would cause "issues" in reading as started out.
abdur rauf
abdur rauf el 29 de Mzo. de 2018
Editada: abdur rauf el 29 de Mzo. de 2018
Finally from your second last comment comment_551318 , i understand what you explained in comment_550301. Actually, i was looking for the kind of explanation in your last comment. Now, i have one small problem about the days in leap years and common year. e.g the 12-July 2004 and 12-July 2007 need to represent different days. But, i do not know that this code differentiate it on not?
Also, i used plot([day day],[start time stop time]) with hold on and get similar plot that from your code. I will post it tomorrow
Thanks for your help.
dpb
dpb el 29 de Mzo. de 2018
Editada: dpb el 30 de Mzo. de 2018
It does not differentiate, no(*). As I was complaining to one of TMW staff at <Comment_550907> there's no way that I can see to incorporate simply an offset time into one of the time objects other than via datenum which I was trying to avoid here for the neophyte.
For the purpose of the graph I doubt it really will make any difference whatsoever as far as the appearance or conclusions that could be drawn simply from a figure but then again, "correct is correct".
The only way to take leap years into account for such a plot would be via logic such as
>> datenum(2004,7,12)-datenum(2003,12,31)
ans =
194.00
>> datenum(2007,7,12)-datenum(2006,12,31)
ans =
193.00
>>
where one reverts back to datenum entirely and ends up plotting the day-of-year on the x-axis and manually create ticks where desired. The problem then is that those ticks are off a day for either the non- or leap-years depending on which count of days you choose for the beginning of each month after February and that has to be so for the reference figure as well (presuming it has a leap year; I didn't go back and check).
There simply is no way to put both on the same scale without an "off-by-one" error for one or the other--hence the reason I ignored it.
And, as a final comment, I'd submit that both are July 12 on the calendar scale in the subject paper as well as on the one produced here; the axis isn't labelled as "days after first of year" but as calendar months and by the calendar they are the same day of their respective years.
(*) Of course, internally it would using the actual year; then you've got the problem of trying to plot on an overlapping scale as each date would be a removed from the others by the number of calendar years along the x-axis its actual date reflects..."can't have cake and eat it, too!" sorta' deal.
ADDENDUM
"The only way to take leap years into account ..."
Actually there is another way if you're just adamant that you want the position of the observation dates for leap years to be plotted at the "day plus one" position on the axis. To do so, before converting to the observed time of reference year, if the observation is within a leap year and after February add one day to the input day. You then get the revised relative position "for free". (As above, I still think it's misguided to do so, but it is your data, not mine, so do as you see fit).
dpb
dpb el 29 de Mzo. de 2018
Editada: dpb el 30 de Mzo. de 2018
As alluded to in above comment and the query to Peter, there is reason to revert to datenum for x-axis as well as for the y that has only to do with the appearance of the plot. I so far have found no way to not display the underlying year (here 2018) on the axis label with the datetimeruler object; I've not yet gone "handle-diving" for hidden properties, but with anything that is exposed to the user there's no way to hide that artifact it seems.
If I get a chance't, I'll try to replot the above using datenum/datetick to illustrate; the data will be the same, just what axis variable is used to plot against and how to label...
ADDENDUM:
comes from same script as above until the following at the plotting stage:
plot(datenum(X(ib(1):ib(2))),H(ib(1):ib(2)),'linewidth',3) % first year segment
hold on
for i=2:length(ib)-1
plot(datenum(X(ib(i):ib(i+1))),H(ib(i):ib(i+1)),'linewidth',3)
end
% dress up plot a little and annotate; much room here for creativity... :)
xlim(datenum([datetime(yr,5,1) datetime(yr,8,1)]))
datetick('x','keeplimits')
ylim([0 24])
set(gca,'ytick',(0:4:24))
legend(num2str(u),'location','southwest')
ylabel('UTC')
Looks the same excepting the x-axis values are now datenums and so must use datetick to format them as dates; otherwise they're just funny-looking doubles. But, this way there's no '2018' to distract things; when I started I was certain there just had to be a way to display just the months labels but as noted above, if there is it's well hid.
abdur rauf
abdur rauf el 30 de Mzo. de 2018
Editada: abdur rauf el 30 de Mzo. de 2018
@ Dear dpb,
Sir, if possible then kindly check my answer to this question. Is this possible to use for loop so that I can get the plot more easily? Also, I have changed the color of the inserted text manually, is this possible to do this by writing command. Also, I have converted the HH:MM:SS format to HH format manually. How to convert an array having both start and end from HH:MM:SS to hours?
Thanks
dpb
dpb el 30 de Mzo. de 2018
Editada: dpb el 30 de Mzo. de 2018
The way to "use a loop" is to read the data from the input file and process it as I illustrated above. One could choose a number of alternatives as to details, but the outline of what to do and how to do it "the Matlab way" is provided in working code.
I let the colors cycle "automagically"; certainly one can choose alternate color order or specific colors programmatically; there are examples in the documentation on plotting/graphics illustrating such. For the latter you must have some method for specifying which color you want with which year if not satisfied with the default colors or order.
As for the labels; use legend; that's what it's for; again use the documentation on how to associate line handles with legend text such that the color association is maintained automatically. There are many other options I didn't use including not drawing the box around it; those again are left as "exercise for the student" :) ; the "how" is all in the documentation and not difficult, just takes reading/looking up the syntax.
abdur rauf
abdur rauf el 28 de Nov. de 2018
Editada: dpb el 28 de Nov. de 2018
Dear dpb,
  • I am using MATLAB 2018a. I am facing a problem using the following command.
dates=datetime(timedata.Date,'InputFormat','ddMMMMyyyy','Format','ddMMMM');
After running this command, I received an error message,
Error using datetime (line 616)
Unable to parse date/time text using the format 'ddMMMMyyyy'. If the date/time text
contain day, month, or time zone names in a language foreign to the 'zh_CN' locale,
those might not be recognized. You can specify a different locale using the 'Locale' parameter.
If possible then kindly give me some suggestion.
Thanks
dpb
dpb el 28 de Nov. de 2018
The error message gave you the suggestion... :)
Where are you located so what is the local time zone name/language?
What does the input data you're using look like?
Dear dpb,
Thanks

Iniciar sesión para comentar.

Más respuestas (2)

abdur rauf
abdur rauf el 30 de Mzo. de 2018
Editada: abdur rauf el 30 de Mzo. de 2018
This plot I get by simply using the number of days and start and end time in hours.
Credit goes to my supervisor Li Hailong, who helped me.
%%%For the year 2004
plot([194 194],[9.04 9.87],'r','linewidth',3); %%%194 represents 12 July 2004 and [9.04 9.87] represents the start and end time in hours.
hold on;
plot([195 195],[8.00 8.96],'r','linewidth',3);
hold on;
plot([195 195],[9.44 9.47],'r','linewidth',3);
hold on;
plot([196 196],[9.57 9.59],'r','linewidth',3);
hold on;
%%%For year 2007
plot([193 193],[9.25 9.45],'b','linewidth',3); %%%193 represents 12 July 2007
hold on;
plot([197 197],[7.52 7.54],'b','linewidth',3);
hold on;
%%%For year 2013
plot([172 172],[9.26 9.28],'g','linewidth',3);
hold on;
plot([173 173],[9.95 9.99],'g','linewidth',3);
hold on;
plot([173 173],[10.68 11.00],'g','linewidth',3);
hold on;
plot([173 173],[11.35 11.97],'g','linewidth',3);
hold on;
plot([173 173],[12.16 12.93],'g','linewidth',3);
hold on;
plot([174 174],[10.10 10.33],'g','linewidth',3);
hold on;
plot([174 174],[12.54 12.81],'g','linewidth',3);
hold on;
plot([207 207],[11.68 11.89],'g','linewidth',3);
hold on;
%%%For year 2015
plot([198 198],[5.80 5.98],'m','linewidth',3);
hold on;
plot([198 198],[6.00 6.50],'m','linewidth',3);
hold on;
plot([198 198],[7.29 7.83],'m','linewidth',3);
set(gca,'xlim',[153 214]);%%%Day number '153' represents 1 June and day number '213' represents 31 July
set(gca,'ylim',[0 24]);
set(gca,'XTick',[153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214]);
set(gca,'XTickLabel',{'Jun','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','Jul','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','Aug'})
text(154,10,'\fontsize{07}\fontname{Arial}2004')
text(154,8,'\fontsize{07}\fontname{Arial}2007')
text(154,6,'\fontsize{07}\fontname{Arial}2013')
text(154,4,'\fontsize{07}\fontname{Arial}2015')
end

4 comentarios

dpb
dpb el 30 de Mzo. de 2018
Editada: dpb el 30 de Mzo. de 2018
set(gca,'XTick',[153 154 ....
set(gca,'XTickLabel',{'Jun','','','...,'','Jul','',...
NB: This has precisely the problem/feature I outlined above of the location of the month relative to the start day number is off-by-one for either the leap year or the nonleap years depending upon which you used to compute the tick mark day number. So, why bother; just use the calendar date and go on; you can't have it both ways on one plot.
I'll also note again that the arbitrary width of the line is far more than the one day "jitter" so the visualization of actual time along the axis is skewed so again you're just fooling yourself in thinking that making the offset by one for the leap year is doing anything significant as far as being able to tell anything from the plot.
The "exercise for the student" to correct that perception is to expand on what I did above except to recast to use fill to draw areas whose width actually does correspond to the length of the period on the x-axis.
If you want to expand the axis somewhat to make the width a little more, use
xlim(datenum([datetime(yr,6,1) datetime(yr,8,1)]))
or whatever range that suits your fancy.
ADDENDUM
If you want to exclude years that don't have duration of observations over some lower bound, introduce what is the minimum time you want to be considered and test for that either while building the X, Y vectors or wait until plotting and put the test in the loop to skip any less than that; no need to do everything by hand.
Of course, if this is just a "one time only" exercise, simply edit the input file to exclude the ones you don't want; if it's something you'll be doing multiple times during the course of continuing research it'll well be worth doing programmatically.
dpb
dpb el 30 de Mzo. de 2018
Editada: dpb el 30 de Mzo. de 2018
I believe with the last revision (posted as separate, second Answer to not replace the earlier) I have done my duty. :)
Your mission, should you choose to accept it... is to look at the code enough to understand what it is doing and how; it is NOT that complicated if you'll use the debugger to look at intermediate steps and such aa the steps that build the X, Y vectors for plotting. Use some shorter sections so you can see what is going on...it's simply using how data are arranged in memory in Matlab in column-major order by default and judiciously creating and then reshaping a 2D array to produce the sequence desired for plotting.
Similarly if you study the building of the index vector for selecting subsections; it's easily worked out with pencil/paper what that actually is doing. Such "tricks" are the keys to really learning to use Matlab as it is intended and to get away from what you've done in entering every individual number by hand; essentially not using any of the capability of Matlab at all. If any data change, you have no choice but to redo it all.
Such machinations seem totally arcane initially, granted, but only by seeing such and finally seeing that fundamentally they're really not that mysterious will one began to get the hang of using Matlab power; beginning to see the patterns behind data to generalize is the key.
Thank you sir,
I am putting things together for your new answer and try to understand it. I come across many commands which i have not seen before.
Once again, thanks for spending your precious time for helping me.
dpb
dpb el 31 de Mzo. de 2018
Glad to help as long as see evidence of trying; just can't always find the time immediately at the time of the question.
The number of available functions in Matlab can be overwhelming initially, granted; it takes "time in grade" to begin to become familiar with the various families of functions for graphics, string/character variables, input/output functions, time/date handling, etc., etc., etc., ... The key is that to learn your way around and to get started one must use the the documentation relentlessly and keep looking for things that might be related to what one is trying to accomplish.
One tool that doesn't get much recognition nor mention any longer with the change to having the documentation webpage-based are the venerable lookfor and help commands. 'help' by itself will list all the various areas and then drilling down to a section will list all the functions of that type on one page with a one-line description of what they do. This is an invaluable way to see what functions exist that may be useful for a given problem area before digging into the details of any particular one that looks like might be useful.
While TMW keeps trying to make things easier for newbies by introducing more high-level abstractions for data types and i/o functions and the like, it also does expand the number of functions to make the maze even more complex from that standpoint -- it's a catch-22 kind of deal or "there is no free lunch"...
Good luck, this should get you on your way; will be interested in seeing your final result...

Iniciar sesión para comentar.

dpb
dpb el 30 de Mzo. de 2018
Editada: dpb el 30 de Mzo. de 2018
Somewhat more enhanced...turned into a function with some input parameters to control some features; uses the same input data file I created before; we've beat that subject to death; use whatever format you choose...just fix up the 'InputFormat' format string to match whatever your chosen form looks like.
function rauf(file,fixLeap,minObs)
% Plot observation durations as season of year grouped by year
% RAUF(file, fixLeap, minObsLength)
% file - input data file
% fixLeap - flag for leap year day adjustment; T--> Do
% minObs - minimum length of observation to plot, seconds
timedata=readtable(file);
refdate=clock; % use current year for the reference
yr=refdate(1); mo=refdate(2); da=refdate(3);
dates=datetime(timedata.Date,'InputFormat','ddMMMMyyyy','Format','ddMMMM'); % dates, display without year
% fix up for leap year if desired; add one to day if after Feb and is leap year
if fixLeap % if ask to make fixup
is=isleapyr(dates) & month(dates)>2; % find in leap year and7 after February
dates(is)=dateshift(dates(is),'start','day','next'); % shift those to next day
end
timedata.Start=datetime(timedata.Start,'InputFormat','HH:mm:ss','Format','HH:mm:ss'); % start time; relative to day's date
timedata.Stop= datetime(timedata.Stop, 'InputFormat','HH:mm:ss','Format','HH:mm:ss'); % stop time...
timedata.Date=datetime(strcat(cellstr(dates),num2str(yr)),'InputFormat','ddMMMMyyyy'); % date in reference year
timedata.ObsTime=timedata.Stop-timedata.Start; % add in observation duration
% now here's the place to introduce a minimum observation time to consider...
isLong=seconds(timedata.ObsTime)>minObs; % logical addressing if long enough
plotdata=timedata(isLong,:); % subset only those long enough to plot
dates=dates(isLong); % original dates to match so have year
% construct line segments to plot
% start by getting unique years and separate list by year
[u,ia]=unique(year(dates)); % unique years, location in dates array first of each year
ib=cumsum([1;3*diff(ia)]); % location in full X,Y vector of start,stop,NaT sets of 3
% each record in start/stop fields is one line segment; introduce NaT to break lines
X=[plotdata.Date.'; plotdata.Date.'; NaT(1,height(plotdata))]; X=datenum(X(:));
Y=[plotdata.Start.'; plotdata.Stop.'; NaT(1,height(plotdata))]; Y=datenum(Y(:));
Y=24*(Y-fix(Y)); % convert to 0:24 hours for plotting
figure
ib=[ib;length(X)]; % augment index array with final index
plot(X(ib(1):ib(2)),Y(ib(1):ib(2)),'linewidth',3) % first year segment
hold on
for i=2:length(ib)-1
plot(X(ib(i):ib(i+1)),Y(ib(i):ib(i+1)),'linewidth',3)
end
% dress up plot a little and annotate; much room here for creativity... :)
hAx=gca; % get axis handle to local variable
xlim([datenum(yr,6,1) datenum(yr,8,1)]) % set axis limits; fixup as wanted
datetick('x','mmm,'keeplimits') % set abbreviated month as label
xtk=hAx.XTick; % retrieve tick values
hAx.XAxis.MinorTickValues=xtk(1):xtk(end); % set minor tick values for day markers
hAx.XMinorTick='on' % turn minor ticks on so show up
ylim([0 24]) % 24-hr scale
hAx.YTick=(0:4:24); % show even tick marks
legend(num2str(u),'location','southwest'),legend('boxoff')
ylabel('UTC')
Call something like
file='abdur.dat';
fixLeap=1;
rauf(file,fixLeap,10*60)
to use the leapyear fixup and set the observation length to 10 minutes (in seconds). The above generated the following--
%

3 comentarios

Oh, I forgot my utility routine isleapyr; you'll need it to run the above--
function is=isleapyr(yr)
% returns T for input year being a leapyear
if isdatetime(yr), yr=year(yr); end
is=eomday(yr,2)==29;
dpb
dpb el 30 de Mzo. de 2018
And one last comment regarding the leap year controversy -- the reference year should really be chosen to be a non-leap year or if were to run the code in a leap year as is it would have the axis already drawn for the "plus one" position of March and all months past. Probably just remove the reference to clock and pick an arbitrary year to use; it really has no bearing on anything other than the Matlab variables need something for a year; the code pays no never mind to what that year is but the syntax needs it.
dpb
dpb el 30 de Mzo. de 2018
Editada: dpb el 30 de Mzo. de 2018
Just glanced over the Answer to see if caught any typos or other foo-pahs after a little while away and one additional comment may be worthwhile -- when building X, Y for plotting, it may not be clear as to why introduced the NaT between each pair of start/stop times when each pair is a line and so one could draw each pair individually just looping "two-at-a-time". The trouble there is as you ran into doing every line individually is that each new line is a new PLOT() line object and therefore the default color will cycle unless you keep track of other things to decide when it should/should not change and what it should change to. By using the NaT/NaN between, as mentioned very early on, PLOT() "raises the pen" at each resulting in a broken line but all elements in the x, y vectors for each call to PLOT() are still the same line object and so have the same color...it is much easier to thus find those elements within each year to plot at the same time if one thought ahead to set the data up so that there would be line breaks where wanted.
Again, what seems arcane or perhaps much effort without appreciating "why" has a very simple reason behind it and a little more work up front saves a lot later on.

Iniciar sesión para comentar.

Categorías

Más información sobre Holidays / Seasons en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 23 de Mzo. de 2018

Comentada:

el 29 de Nov. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by