MATLAB: Extracting the year from a timestamp in a column

datedatetimeloopyear

I have a set of 477 values in a column, each that are formatted 'yyyy-mm-dd HH:MM:SS' and am trying to extract only the year using a loop
for i=1:477
[t] = datevec(data{i,8},'yyyy-mm-dd HH:MM:SS');
't_years' = t.Year;
end

Best Answer

Your code confuses a date vector (which is a simple double matrix of size Nx6 or Nx3) with a datetime object (which has the property obj.Year). If you want to use that property then you will need to use a datetime object. If you want to use datvec, then you do not need a loop as it accepts a cell array of char vectors directly. So probably something like this will work (it depends on how the data is arranged in data):
mat = datevec(data(:,8),'yyyy-mm-dd HH:MM:SS');
year = mat(:,1);