Binning problem when using datenum with pcolor

1 Ansicht (letzte 30 Tage)
Lukas
Lukas am 6 Mai 2015
Kommentiert: Larissa Perez am 12 Okt. 2016
Hello everyone.
When I plot data using pcolor using ("contemporary") datenum values for the x-axis, pcolor bins the data into one or a few bins together, instead of using the ones given.
An example code which reproduces this problem:
secinday = 24*3600;
t0vec = [2015 05 01 0 0 0];
t0 = datenum(t0vec);
% Create an interval of an hour of timesteps of a second
t = t0 + (0:(1/secinday):(1/24-1/secinday));
y = 1:10; % Just some y-axis values
z = rand(10,3600); % Random z-values
pcolor(t,y,z);
surf has the same problem. Anyone know why this happens and how I can prevent it? Or how I can work around it?
It is funny that the problem disappears when one makes the y-axis logarithmic:
set(gca,'yscale','log');
shading flat; % Otherwise only gridlines visible
But as soon as you make them linear again, the problem reappears.
Note that it only happens for high datenum values. When one takes for example
t0 = [1 05 01 0 0 0];
there is no problem. So my guess is that it has something to do with the small relative difference between a "contemporary" datenum value and the value a second later. So the workaround I currently use is to do t-floor(t0);
  3 Kommentare
Chad Greene
Chad Greene am 19 Mai 2015
Interesting. I get the same problem on 2012b. Oddly enough,
t = (1:3600) + floor(now);
works, but
t = datenum(2015,5,1,0,0,1:3600);
does not.
Lukas
Lukas am 19 Mai 2015
I think
t = (1:3600) + floor(now);
works because here your difference between every time point is a full day, as opposed to the second, where it is seconds. The unit of Matlab datenum is a day...

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 19 Mai 2015
Bearbeitet: Jan am 19 Mai 2015
With linear indices Matlab uses the OpenGL renderer, while the logarithmic axis enables another oen (painters or zbuffer depending on teh data and which Matlab version you are using).
The OpenGL renderer converts the values to the type single, or "float" in the underlying C-code. datenum values are large and with the single precision the values might be rounded to the same number.
Try to set the renderer of the figure manually and:
t = double(single(t));
After this line the effects of the renderer should vanish - when the renderer is the problem. If so, TMW cannot fix this.
  2 Kommentare
Lukas
Lukas am 19 Mai 2015
You're right. It seems to be a problem of the renderer. If I use e.g. painters, everything is fine. Converting datenums of current dates to singles would indeed lead to zero difference when the difference is a second. Thanks.
Larissa Perez
Larissa Perez am 12 Okt. 2016
I am new to Matlab and am going through a very similar problem. However, turning the datenum variable to single is not solving it. Can anyone help me?
time1 = datenum(measure1.time);
time1 = double(single(time1));
t1 = datestr(measure1.time,'HH:MM:SS');
subplot (3,1,1)
h=pcolor(time1,depth,transpose(measure1.data));
colorbar caxis ([20 22])
c = colorbar;
c.Label.String = 'Temperature [°C]';
ylabel ('Depth[m]');
set(h, 'EdgeColor', 'none');
set(gca,'YDir','reverse');
set(gca,'XTicklabel',t1);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by