How to read year from timeseries?

조회 수: 6 (최근 30일)
Devon Fisher-Chavez
Devon Fisher-Chavez 2020년 1월 28일
댓글: Devon Fisher-Chavez 2020년 1월 29일
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
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);
Devon Fisher-Chavez
Devon Fisher-Chavez 2020년 1월 29일
@Mohammad Sami, that works with my existing program, thanks!
@Walter Roberson, that also works, but will require I change quite a bit of my existing code. Because this is the "proper" way to do things, I will try to convert!
Thank you both for your responses!

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by