MATLAB: Finding intersection points with refline

plot

Im trying to find the intersection points of a plot and a refline#
The reflines are the straight horizontal lines in the figure
I have attached the figures
Cant seem to figure out the code for it
Thanks in advance

Best Answer

It would be easier if you defined both of your functions in each plot with the same X-data. Then you could use the much more efficient code in How to change axis of graph and interpolate data.
This works for today’s plots (I tested it on all of them):
% GET INFORMATON FROM FIGURE:
openfig('Figure(7).fig');
h1c = get(gca, 'Children');
Xdc = get(h1c, 'XData');
Ydc = get(h1c, 'YData');
maxlen = max(cell2mat(cellfun(@max, cellfun(@size, Xdc, 'Uni',0),'Uni',0)))
Xd2 = cell2mat(Xdc(2));
Yd2 = cell2mat(Ydc(2));
Xd = Xd2;
Yd1 = cell2mat(Ydc(1));
Yd = [Yd1(1)*ones(size(Xd2)); Yd2];
% CALCULATIONS:
Ydn = diff(Yd, [], 1); % Subtract line from curve to create zero-crossings
Zx = circshift(Ydn, [0 1]) .* Ydn; % Use circshift to detect them
Zxi = find(Zx < 0); % Their indices
for k1 = 1:length(Zxi) % Use interp1(Y,X,0) to get line intercepts as Xzx
Xzx(k1) = interp1([Ydn(Zxi(k1)-1) Ydn(Zxi(k1))], [Xd(1,Zxi(k1)-1) Xd(1,Zxi(k1))], 0);
end
% PLOT ZERO-CROSSINGS ON FIGURE TO CHECK:
hold on
plot(Xzx, repmat(Yd(1,1),1,length(Xzx)), '*r')
hold off