How to find intersection points between plot and straight line

I tried to find the intersection points of the attached figure using the following code provided to me which worked for a different figure.
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
It returns with the error
Error using cellfun
Input #2 expected to be a cell array, was double instead.
And Im not quite sure how to fix it

 Risposta accettata

That is my code, so I sort of remember what I was doing when I wrote it here nine months ago (in R2014a). What version of MATLAB are you using?
I’ll do my best to help, but I can’t figure out what you want to do. I don’t see a reference line in ‘Curve figure.fig’.

6 Commenti

Hi, thanks for responding, The Reference Line is there when I open the figure( the straight line at around y=1.7) Also I am using matlab versiom r2014a but would like the code to work for version 2013a
My pleasure.
I opened R2013a to see if there was a problem or some sort of incompatibility with R2015a, since I couldn’t see the horizontal (black) line in R2015a, but could in R2013a. The code worked with your previous figure (‘Figure(7).fig’) because I got two values each for both 'XData' and 'YData' with it, one for the line and one for the curve.
With the figure in this (current) post (‘Curve figure.fig’), I’m only getting the horizontal line. I’m not getting any data for the blue curve, either in R2013a or R2015a. For some reason, the blue curve just isn’t present in the figure data. I checked both ‘Curve figure.fig’ you posted to see if the second one was an update of the first, and to see if I could recover the blue curve from it, but they both appear to be the same.
Please check it to see if you’re getting the same result. (Compare it with the figures from your previous post. I linked to it, so you can click on that link to open it.) Then re-plot the current figure, being sure the data for both the line and the curve are present in it, and re-post it. At the very least, I need the data for the blue curve. I can create a constant line at 1.7.
Would it work if I just posted the code that let me get to the figure as well as the excel data
Rawdata=xlsread('PHAS2444.xlsx')
FrequencykHz=Rawdata(:,1)
Voltage=Rawdata(:,4)
Uncertainty=Rawdata(:,7)
scatter(FrequencykHz,Voltage);
figure(1);
errorbar(FrequencykHz,Voltage,Uncertainty,'xr');
figure(2);
hold on;
xx = 15:0.01:55;
yy = spline(FrequencykHz,Voltage,xx);
plot(xx,yy);;
Halfpowerpoints=max(yy)/sqrt(2);
plot(xx,Halfpowerpoints);
hold off;
This is kludgy code, but if it’s one-off, I’m happy enough with it:
Rawdata = xlsread('Vaultec PHAS2444.xlsx');
FrequencykHz=Rawdata(:,1);
Voltage=Rawdata(:,4);
Uncertainty=Rawdata(:,7);
scatter(FrequencykHz,Voltage);
figure(1);
errorbar(FrequencykHz,Voltage,Uncertainty,'xr');
figure(2);
xx = 15:0.01:55;
yy = spline(FrequencykHz,Voltage,xx);
hold on
plot(xx,yy);;
Halfpowerpoints=max(yy)/sqrt(2) * ones(1,length(xx));
plot(xx,Halfpowerpoints);
Q1 = yy - Halfpowerpoints;
Q1csm = Q1 .* circshift(Q1, [0 -1]);
Q1zx = find(Q1csm < 0);
HPPx(1) = interp1(Q1(Q1zx(1):Q1zx(1)+1), xx(Q1zx(1):Q1zx(1)+1), 0);
HPPx(2) = interp1(Q1(Q1zx(2):Q1zx(2)+1), xx(Q1zx(2):Q1zx(2)+1), 0);
plot(HPPx, [1 1]*Halfpowerpoints(1), 'bp', 'MarkerFaceColor','b')
hold off;
The x-values for Half-Power Points are: 34.8 and 38.6 (to three significant figures).
Thank you very much,
So the reason your previous code didnt work was it because of a version issue?
My pleasure.
I don’t believe it was a version issue, but that for some reason the blue curve didn’t appear as 'XData' or 'YData' when I tried to get them from the figure. (The half-power line may have overwritten them somehow, without erasing the blue curve.) I didn’t explore that in my latest code, since I had all the data in the spreadsheet file. You may want to, because a .fig file is actually a very good way to transport both the data and the concept you want to (literally) illustrate.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su 2-D and 3-D Plots in Centro assistenza e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by