How to read year from timeseries?
6 ビュー (過去 30 日間)
古いコメントを表示
Hi there,
I have a script that uses "eval" to define a range of yearly environmental values over a large number of sites for a large number of years. I know "eval" is not suggested, but with the number of years, sites, and variables I am dealing with it makes things a whole lot easier.
Anyways, the following snippet of code works well but I would like to automate it even more. In the following, I first load data then create a structure (pars_yr) that defines the variable years I am looking at.
% Download .DAT data matrix, define first column as datetime timeseries
%third column is temp. "Date_all_site" is the entire datetime column for that site.
% Data is recorded every 30min throughout the year
pars_site_yr = {'2014','2015','2016','2017','2018','2019'};
site_yr_start = min(Date_all_site):calyears(1):max(Date_all_site);
site_yrs = years(site_yr_start)
for i = 1:length(pars_site_yr)
eval(['temp_site_',pars_site_yr{i},' = site_',pars_yr{i},'(3,:);'])
end
% more of my code
The problem is, every time I add more data I need to manually change "pars_site_yr". This isn't a huge problem considering I will only need to do it once a year, but I think it would be cool if the code just automatically "read" the new datetime years (especially considering I already defined the years for each site in site_yrs).
"site_yrs" has all the years based off the input "Date_all_site", but it is not in a useable "structure" form like pars_site_yr is. Each site has a different range of years that data was recorded.
I'm pretty new to matlab so hopefully this is easy. Thanks a ton for all your help!
-Devon
5 件のコメント
Mohammad Sami
2020 年 1 月 29 日
編集済み: Mohammad Sami
2020 年 1 月 29 日
Assuming a datetime variable. you can use year function
yr = year(datetime_var);
yr = unique(yr); % this will give you all the years in your data.
pars_site_yr = arrayfun(@num2str,yr,'UniformOutput',false);
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!