how can i plot data of different types using matlab plot(x,y) ?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponses (3)
michael scheinfeild
le 26 Fév 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
le 26 Fév 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 commentaires
Star Strider
le 26 Fév 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.
Brendan Hamm
le 26 Fév 2015
Modifié(e) : Brendan Hamm
le 26 Fév 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 commentaires
Brendan Hamm
le 26 Fév 2015
Modifié(e) : Brendan Hamm
le 26 Fév 2015
Oops. Those braces should be parenthesis. My apologies. I have updated the solution.
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!