MATLAB Answers

How can I set time for x-axis plot in standard year, month,

5 views (last 30 days)
Farshid Daryabor
Farshid Daryabor on 17 Mar 2020
Answered: Peter Perkins on 14 Apr 2020
Please find the attached files I want to display time in form of 2017-04, 2017-07, etc. for x-axis. I have trying the following command but is not what I expected, The time reference since 1950-01-01T00: 00: 00Z. eg, 67 + 1950 = 2017
datetick ('x', 'yyyy-mm', 'keeplimits')

  3 Comments

Hiro
Hiro on 17 Mar 2020
You should use the datetime vector for your x-axis.
This type is designed for "time" and "duration". If you didn't use it you would end up experiencing a lot of hassle and bustle.
Farshid Daryabor
Farshid Daryabor on 17 Mar 2020
Dear Hiro,
I did follow your command, based on the starttime and endtime,
nc.time_coverage_start = ncchar(''2016-11-02T07:58:00Z'');
nc.time_coverage_end = ncchar(''2018-09-10T12:11:00Z'');
>> t1 = datetime(2016,11,02,07,0,0);
>> t2 = datetime(2018,09,10,12,0,0);
>> tx = t1:t2;
It doesn't work for ploting "pcolor(datenum(tx),-depth,temp);" because "tx(1*678)" is not same size of "depth(143*263)" and "temp(143*263)"
>> pcolor(datenum(tx),-depth,temp);
Error using pcolor (line 59)
Matrix dimensions must agree.
So the only way is to extract the time (in number) from netcdf file which is same size with depth and temp and plot
>> size(time)
1*263
>> size(depth)
143*263
>> size(temp)
143*263
>> pcolor(tx,-depth,temp);
>> datetick('x', 'yyyy-mm', 'keepticks')
Farshid Daryabor
Farshid Daryabor on 17 Mar 2020
please find attached time, I really appreciate any suggestion.

Sign in to comment.

Answers (1)

Peter Perkins
Peter Perkins on 14 Apr 2020
I have no idea what that mat file is intended to contain. It's one double vector.
In any case, don't use datetick. Just use datetime plotting:
>> t = datetime(2020,1,1:100:1000);
>> y = 1:20;
>> pcolor(t,y,rand(length(y),length(t)))
Normally, you could set the datetime format to what you need, but your case is ... unusual. You'll need to make the tick labels by hand. Get the tick locations, get the offset from 1950 in years and months, and build the labels.
>> ax = gca;
>> t0 = datetime(1950,1,1);
>> ticks = ax.XTick;
>> [y,m] = split(between(t0,ticks),["years","months"])
y =
70 70 71 71 72
m =
0 6 0 6 0
>> xlabs = compose("%04d-%02d",[y',m'])
xlabs =
5×1 string array
"0070-00"
"0070-06"
"0071-00"
"0071-06"
"0072-00"
>> ax.XTickLabel = xlabs;

  0 Comments

Sign in to comment.


Translated by