This answer was inspired and modified from an orginal answer by @Matt J! The original intent was to place NaN where there was missing data points with reference to a global time stamp. There were 3 main issues with completion under the code that was the most recent (in comments). For those that may need this in the future, here are the issues, and then the solution will be posted at the end of this breakdown.
There were issues in compilation, as the line:
y1(I)=data{i}(I,2);
caused an error when x did not exactly equal x1, as length(I) was greater than length(x), so it attempted to pull from data that did not exist. The assertion built in did not catch that as written.
Additionally, the first of the array in data that generated x1 was not necessarily the maximum, which assisted the previous issue.
Finally, the NaN that was built in to the previous code put NaN's at the end of the array, which would not tell me which timestep they pertain to. I am not as well versed as Matt J, so I did not know how to eloquently fix these. I am sure there are steps to shorten this, and prevent nested for and if statements, but are above my current coding knowledge. The solution to all of those, and creation of a plot based on all non NaN data (the end goal) is below. Thank you again Matt J for your help in a lot of these steps, I couldn't have done it without you.
celldata_PC3=Data.m(idxPC3);
celldata_PC3=[celldata_PC3{:}];
celldata_PC3=[celldata_PC3.n];
for i = 1:length(celldata_PC3)
a{i} = length(celldata_PC3{1,i});
end
b = max(cell2mat(a));
b_indx = find(b == cell2mat(a));
x1=celldata_PC3{b_indx(1)}(:,1); %this will be the global timestep all other x will be
% compared to to define where NaN should be located.
for i=1:numel(celldata_PC3)
xPC3=celldata_PC3{i}(:,1);
yPC3=celldata_PC3{i}(:,2);
I=ismember(x1,xPC3);
assert(sum(I)==length(xPC3),'Assumption failed: x(:,t) is not a perfect subset of x(:,1)')
NanIdx = find(I == 0);
Fixed_x = xPC3; %initialize so that for loop can detect initial length
Fixed_y = yPC3; %initialize so that for loop can detect initial length
for j = 1:length(NanIdx)
if ismember(1, NanIdx) %Using these nested if statements are necessary for 3 conditions of NaN location
if NanIdx(j) == 1
Fixed_x = [NaN; xPC3];
Fixed_y = [NaN; yPC3];
else
if NanIdx(j)>length(Fixed_x)
Fixed_x = [Fixed_x; NaN];
Fixed_y = [Fixed_y; NaN];
else
Fixed_x = [Fixed_x(1:NanIdx(j)-1); NaN; Fixed_x(NanIdx(j):end)];
Fixed_y = [Fixed_y(1:NanIdx(j)-1); NaN; Fixed_y(NanIdx(j):end)];
end
end
else
if NanIdx(j)>length(Fixed_x)
Fixed_x = [Fixed_x; NaN];
Fixed_y = [Fixed_y; NaN];
else
Fixed_x = [Fixed_x(1:NanIdx(j)-1); NaN; Fixed_x(NanIdx(j):end)];
Fixed_y = [Fixed_y(1:NanIdx(j)-1); NaN; Fixed_y(NanIdx(j):end)];
end
end
end
Fixed_celldata_PC3{i}=[Fixed_x,Fixed_y];
end
Fixed_celldata_PC3 = [Fixed_celldata_PC3{:}]
all_xPC3 =Fixed_celldata_PC3(:,1:2:end);
all_yPC3 =Fixed_celldata_PC3(:,2:2:end);
figure
hold on
for i= 1:length(all_xPC3)
plot(all_xPC3(:,i), all_yPC3(:,i), 'Color', [1 0 0 0.2], 'HandleVisibility', 'off')
end
medAlexaPC3 = median(all_yPC3,2, 'omitmissing');
plot(x1, medAlexaPC3, 'Color', [0.75 0 0], 'LineWidth', 3, 'LineStyle', ':', 'DisplayName', 'PC3 Median')