how can i plot data of different types using matlab plot(x,y) ?

10 views (last 30 days)
Hi
after importing my data into matlab as a cell array, i am wondering how to plot my time series. The issue is coming from the treatment of my date column (first column) which is imported as text. i did not experience any issue with the remaining columns since i used cell2mat to convert cells data into double (requirement for plotting).
any idea would be appreciated. Thanks

Answers (3)

michael scheinfeild
michael scheinfeild on 26 Feb 2015
you can try to conver each of cell array by split 23/05/15 to 3 parts by search "/" the you can code it to index = year*10+month*10+day the plot it

Star Strider
Star Strider on 26 Feb 2015
I don’t know what your cell array looks like once you have imported your data. Taking a guess, this is how I would do it:
C = {['1/27/2015';'1/26/2015';'1/23/2015';'1/22/2015';'1/21/2015';'1/20/2015'], rand(6,1)*108,rand(6,1)};
DN = datenum(C{1}, 'mm/dd/yyyy');
figure(1)
subplot(2,1,1)
plot(DN, C{2})
datetick('x', 'mm/dd/yyyy')
set(gca, 'FontSize', 8) % Optional
grid
subplot(2,1,2)
plot(DN, C{3})
datetick('x', 'mm/dd/yyyy')
set(gca, 'FontSize', 8) % Optional
grid
  2 Comments
Isma
Isma on 26 Feb 2015
hi Thanks for the feedback. as my data is (2945*3), could your syntax handle with C={[data(:,1),rand(6,1)*108,rand(6,1)}; also could you explain me a bit about why plot(DN, C{2})? acknowledging DN is my x-axis. why C{2} or could you confirm C{2} = rand(6,1)*108,rand(6,1)+ Y-absciss for my understanding ? cheers
Star Strider
Star Strider on 26 Feb 2015
If your data are all in one cell, you would have to do the subscripting differently, but the rest of my code should work. I don’t have your data to work with, so I can’t be more specific.

Sign in to comment.


Brendan Hamm
Brendan Hamm on 26 Feb 2015
Edited: Brendan Hamm on 26 Feb 2015
If your version is 2014a or less then you can use (assuming your cell array is named 'x')
% This converts date strings to MATLAB serial date numbers
myDate = datenum(x(:,1));
plot(myDate,y); %for some numeric vector y
datetick
There are more options you can use for datenum and datetick so look at the documentation.
If your version is later than this (currently only 2014b is) then you can use the new datetime variable.
myDate = datetime(x(:,1),...); % You will need to provide the input format thus the ...
% datetimes can be plotted directly:
plot(myDate,y);
  2 Comments
Isma
Isma on 26 Feb 2015
hi your solution seems to be the quickest and easiest to implement as my cell data dimension is (2945*3). however
myDate=datenum(abrxeur{:,1})
Error using datenum
Too many input arguments.
any idea? i am using version 2013a version
Brendan Hamm
Brendan Hamm on 26 Feb 2015
Edited: Brendan Hamm on 26 Feb 2015
Oops. Those braces should be parenthesis. My apologies. I have updated the solution.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!