MATLAB: Converting yyyymmdd double to decimal year

converting datedatedecimal yeardoublesyyyymmdd

Hi there, I have many variables made from doubles. I would like to plot certain variables against the date which is also made of doubles in the format yyyymmdd. So far i have:
function [out1] = simpleplot(variable,depthvar,mindepth,maxdepth,datevar,startdate,enddate,xlab,ylab);
depth = find(depthvar>mindepth & depthvar<maxdepth & ~isnan(variable));
datedepth=datevar(depth);
variable_depth=variable(depth);
plot(datedepth, variable_depth,'sk--')
xlabel(xlab);
ylabel(ylab);
axis([startdate enddate min(variable_depth) max(variable_depth)]);
end
However because my dates are yyyymmdd not in decimal year the plot has large gaps in it.
How can i convert the date doubles to decimal year within the function and also to have the x axis show date in a meaningful way like yy/mm rather than decimal year.
thank-you

Best Answer

An alternative to the conversion to string and back, which is always going to be slow, is to divide and mod you date variable:
datevar = [19981020 19981023 19981203 1999011];
y = floor(datevar/10000);
m = floor(mod(datevar, 10000) / 100);
d = mod(datevar, 100);
datedepth = datetime(y, m, d);