how can i plot data of different types using matlab plot(x,y) ?
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(3 个)
michael scheinfeild
2015-2-26
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
2015-2-26
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 个评论
Star Strider
2015-2-26
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
2015-2-26
编辑:Brendan Hamm
2015-2-26
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 个评论
Brendan Hamm
2015-2-26
编辑:Brendan Hamm
2015-2-26
Oops. Those braces should be parenthesis. My apologies. I have updated the solution.
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!