Plotting precip and time data
3 次查看(过去 30 天)
显示 更早的评论
Hi There, This is going to come across as a very simple question but i want to plot time on the x-axis in 'yyyy/mm/dd' format and precipitation on the y axis. I'm reading files from an excel data sheet but when i plot it using attached code, the x-axis looks weird. I know it has to do with the formatting but i'm not sure what... Any help is much appreciated! Thanks, Natasha
prcp_hope = xlsread('NCEI_CDO.xlsx','Hope','G:G'); prcp_hope(prcp_hope == -9999) = NaN;
datem_hope = xlsread('NCEI_CDO.xlsx','Hope','F:F');
plot(datem_hope,prcp_hope)
0 个评论
采纳的回答
KSSV
2017-4-11
You check your file's date for the row number 29 and 620..it is not correct. I think it should be corrected. Try the code with attached file.
3 个评论
KSSV
2017-4-12
prcp_hope = xlsread('NCEI_CDO.xls','Hope','G:G');
prcp_hope(prcp_hope == -9999) = NaN;
datem_hope = xlsread('NCEI_CDO.xls','Hope','F:F');
dates = datestr(datem_hope) ;
date = num2str(datem_hope) ;
date = strcat(date(:,1:4),'/',date(:,5:6),'/',date(:,6:end)) ;
thedatenum = datenum(date) ;
plot(thedatenum,prcp_hope)
datetick('x','yyyy/mm/dd')
更多回答(1 个)
Peter Perkins
2017-4-12
编辑:Peter Perkins
2017-4-12
If you are using a fairly recent version of MATLAB, use readtable, not xlsread. To make your life easier, you'll want to move those column headers in your Excel file to be above the data, not alongside the data. It looks like your dates are numbers of the form yyymmdd, so call datetime with 'Convert','yyyymmdd' to get a datetime variable in the table. Then just call plot with that datetime and whatever data you want to plot. Something like
>> t = readtable('NCEI_CDO.xls');
>> t.DATE = datetime(t.DATE,'ConvertFrom','yyyymmdd');
>> plot(t.DATE,t.PRCP);
except that you have some work to do between lines 2 and 3, because you have some dates that are in the 900's, rather than the 1900's, and some PRCP values that are -9999. There are simple tools like standardizeMissing to handle the latter.
Hope this helps.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!