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)

采纳的回答

KSSV
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
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')
Natasha Sekhon
Natasha Sekhon 2017-4-12
Thank you so much! One last question, i've attached the computed figure and something seems to be off with the placement of the data. For example, if you look closely to 1980's there is criss cross of the line which shouldn't happen if each time has one corresponding precip value.

请先登录,再进行评论。

更多回答(1 个)

Peter Perkins
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.

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by