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

7 vues (au cours des 30 derniers jours)
Jonathan Macuroy
Jonathan Macuroy le 3 Juil 2019
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 commentaires
Rik
Rik le 3 Juil 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.
Jonathan Macuroy
Jonathan Macuroy le 3 Juil 2019
I see. Thank you very much! :)

Connectez-vous pour commenter.

Réponse acceptée

Peter Perkins
Peter Perkins le 3 Juil 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 commentaire
Jonathan Macuroy
Jonathan Macuroy le 4 Juil 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!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by