Plotting precip and time data

3 ビュー (過去 30 日間)
Natasha Sekhon
Natasha Sekhon 2017 年 4 月 11 日
コメント済み: Natasha Sekhon 2017 年 4 月 12 日
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 Center および File ExchangeMarine and Underwater Vehicles についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by