MATLAB: Parsing a vector by NaN into separte, unequal vectors into a cell array

vector nan parse

I'm trying to parse a vector with NaN, into a cell array that is delineated by the start and stop of the NaN
example = [1 2 3 Na Na 2 3 Na 4 5 6 7 Na Na Na Na 1];
and turn that into
answer = {[1 2 3] [2 3] [4 5 6 7] [1]}
any suggestions? I've been using find(isnan(example)) to get the indices of NaN, but what I really need are the stop and stop indices of the non – NaN segments
Thanks!

Best Answer

EDIT
Here's a fully vectorized engine that keeps original indices. This requires the Image Processing Toolbox.
x = [1 2 3 nan nan 2 3 nan 4 5 6 7 nan nan nan 1];
notnans = ~isnan(x);
idx = bwlabel(notnans);
n = histcounts(idx);
oneToN = 1:numel(x);
C = mat2cell([x(notnans); oneToN(notnans)],2,n(2:end))