I obtained the bwperimtrace function from:
https://www.mathworks.com/matlabcentral/fileexchange/49551-trace-perimeter-of-binary-image-with-line-segments--bwperimtrace-
I changed lines 32 and 33 of the bwperimtrace function from:
xpix = xlims(1):dx:xlims(2); ypix = ylims(1):dy:ylims(2);
to
xpix = xlims(1):dx:xlims(end); ypix = ylims(1):dy:ylims(end);
Then I ran the following:
% Generate data mydata = magic(10) ; myoverlay = mydata>50 ;
%Generate trace around pixels
cellout=bwperimtrace((myoverlay-1)*-1)
figure;
for i = 1:1:length(cellout)
plot(cellout{i}(:,1),cellout{i}(:,2))
hold on
end
