How can I plot datetime as x-axis in pcolor ?

Hello Seniors,
I have time(datetime - 6047X1) need to be in x-axis, height(numeric value - 27X1) as y axis, C(27X6047) numeric values.
I am having error(Error using pcolor (line 65) Data inputs must be real.) while I want plot by using pcolor.

4 Comments

what's the datatype for 'time', 'height' & 'C' ? If anyone of them is complex, then I think that might not be allowed for 'pcolor'.
time is datetime(ddmmyyyy hh:mm:ss) and that one shows error like
"Error using pcolor (line 65)
Data inputs must be real."
That's my question "How can I plot datetime as x-axis" ?
if pcolor won't work then what function works ?
One of the way can be:
dt = [datetime('yesterday'),datetime('today'),datetime('tomorrow')];
y = 1:3;
C = [3 4 5; 1 2 5; 5 5 5];
pcolor(datenum(dt),1:3,C)
a = gca;
a.XTick = datenum(dt);
a.XTickLabel = datestr(datenum(dt));
I am not sure if that's what you want but let me know if you have doubts.
Thanks a lot it worked.
A small question that is any simple command to see specific dates from whole time.
for example : I have time scale 01/01/2015 to 10/02/2015. And now I want plot only 10/01/2015 to 15/01/2015. (with/without using logical index).

Sign in to comment.

 Accepted Answer

Adam Danz
Adam Danz on 14 Oct 2019
Edited: Adam Danz on 14 Oct 2019
There must be imaginary data in your inputs. Here's a demo with valid datetime inputs.
dt = [datetime('yesterday'),datetime('today'),datetime('tomorrow')];
y = 1:3;
C = [3 4 5; 1 2 5; 5 5 5];
pcolor(dt,1:3,C)
To determine if imaginary data exist anywhere within a numeric array, use isreal() which will either return a TRUE or FALSE indicating whether all of the data in your array are real numbers. If it returns a false, you can use imag() to find the element that is not real.
Note that pcolor() did not accecpt datetime values prior to r2019b.
In current documentation you can see that datetime is listed as an accepted data type. In r2019a documentation, pcolor does not list datetime as an accepted input. Using datetime as an input to pcolor() in r2019a or earlier will result in the error, "Data inputs must be real."

5 Comments

"Sorry Here again the error occured for sample code. And yes datetime is not real.
That is my question "How can I plot datetime in pcolor""
What release of matlab are you using?
I just updated my answer. Prior to r2019b you cannot use datetime as an input to pcolor.
If you don't have access to r2019b, you can convert your datetime data to date-number data using datenum() and the provide that as input to pcolor().
After that you can try to use datetick() to add the datetime labels or you can just change the xTickLabels to datetime strings by converting your datetime to strings using datestr().
Yeah r2019b have direct access to plot datetime in x-axis.
Thanks a lot it worked.
A small question that is any simple command to see specific dates from whole time.
for example : I have time scale 01/01/2015 to 10/02/2015. And now I want plot only 10/01/2015 to 15/01/2015. (with/without using logical index).
Logical indexing would be best. Suppose your date vector is named dt,
idx = dt>=DATE1 & dt <=DATE2;
Then you could set those values to NaN,
x(idx) = NaN;
y(idx) = NaN;
pcolor(x,y,C)

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!