How do I plot a datetime vs data within a cell array?

6 views (last 30 days)
Hello! Say I have a nx2 cell array named CELL with the following data types per column:
column 1: datetime
column 2: double
Whenever I try to plot using plot(CELL{:,1},CELL{:,2}), the following error occur:
"Data inputs must match the axis configuration. A numeric axis must have numeric data inputs or data inputs which can be converted to double. "
NOTE: I also tried cell2mat(CELL{:,1}) to try if I can convert the datetime column into an array but the following error occurs:
"CELL2MAT does not support cell arrays containing cell arrays or objects."
May I ask how I can plot the datetime cell column against the double cell column? Thank you very much in advance.
  2 Comments
Rik
Rik on 3 Jul 2019
You must either convert your data to single arrays, or use a loop. Note that {:} creates a comma separated list, so you should use normal parentheses in your call to cell2mat.

Sign in to comment.

Accepted Answer

Peter Perkins
Peter Perkins on 3 Jul 2019
You almost certyainly do not want to store your data like that. For one thing, cell arrays are designed for complete generality, anything can be in any element, so you will find things that involve homogeneous data to be clumsy or unworkable (as you found). For another, the memory footprint will be much larger.
Use a table, or in this case a timetable. Not sure how you got where you are, but it's easy to convert. Say you have this cell array:
>> C
C =
10×2 cell array
{[01-Jul-2019]} {[0.81472]}
{[02-Jul-2019]} {[0.90579]}
{[03-Jul-2019]} {[0.12699]}
{[04-Jul-2019]} {[0.91338]}
{[05-Jul-2019]} {[0.63236]}
{[06-Jul-2019]} {[0.09754]}
{[07-Jul-2019]} {[ 0.2785]}
{[08-Jul-2019]} {[0.54688]}
{[09-Jul-2019]} {[0.95751]}
{[10-Jul-2019]} {[0.96489]}
>> TT = table2timetable(cell2table(C,'VariableNames',{'Time','X'}))
TT =
10×1 timetable
Time X
___________ _______
01-Jul-2019 0.81472
02-Jul-2019 0.90579
03-Jul-2019 0.12699
04-Jul-2019 0.91338
05-Jul-2019 0.63236
06-Jul-2019 0.09754
07-Jul-2019 0.2785
08-Jul-2019 0.54688
09-Jul-2019 0.95751
10-Jul-2019 0.96489
At that point, plotting is simple.
>> plot(TT.Time,TT.X)
But better still, create the timetable to begin with, instead of the cell array.
  1 Comment
Jonathan Macuroy
Jonathan Macuroy on 4 Jul 2019
Peter,
Hello! Yeah, I believe your suggestion to not use cell arrays when working with data is the right path. I instead created separate tables for them, with the datetime as a datetime array instead of a cell array. Thank you very much!

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!