Plotting precip and time data

3 views (last 30 days)
Natasha Sekhon
Natasha Sekhon on 11 Apr 2017
Commented: Natasha Sekhon on 12 Apr 2017
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)

Accepted Answer

KSSV
KSSV on 11 Apr 2017
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 Comments
KSSV
KSSV on 12 Apr 2017
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 on 12 Apr 2017
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.

Sign in to comment.

More Answers (1)

Peter Perkins
Peter Perkins on 12 Apr 2017
Edited: Peter Perkins on 12 Apr 2017
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.

Community Treasure Hunt

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

Start Hunting!