필터 지우기
필터 지우기

How to find intersection points between plot and straight line

조회 수: 2 (최근 30일)
Vaultec
Vaultec 2015년 3월 22일
댓글: Star Strider 2015년 3월 23일
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

채택된 답변

Star Strider
Star Strider 2015년 3월 22일
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
Vaultec
Vaultec 2015년 3월 23일
Thank you very much,
So the reason your previous code didnt work was it because of a version issue?
Star Strider
Star Strider 2015년 3월 23일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by