Finding intersection points with refline
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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
採用された回答
Star Strider
2014 年 6 月 30 日
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
8 件のコメント
Vaultec
2014 年 7 月 1 日
just a quick question what does the maxlen = max(cell2mat(cellfun(@max, cellfun(@size, Xdc, 'Uni',0),'Uni',0))) that line do?
Star Strider
2014 年 7 月 1 日
編集済み: Star Strider
2014 年 7 月 1 日
That line finds the maximum of the lengths of the 'XData' vectors. I initially used it to determine the argument to the ones function that creates a vector of y values for the constant line to use in the call to interp1. The values are in cell arrays, explaining the nested calls to cellfun.
Thanks!
How would i go about if I wanted to have the intersection points be placed as a matrix/ or find the values of the intersection points?
Star Strider
2014 年 7 月 2 日
The x-coordinates of the intersections are in the Xzx vector. (That is the same as for the code in your earlier question as well. I used essentially the same code for both.) The y-values of the intersections are the value of the constant line. If you want a matrix of the x and y coordinates of the intersections, create it as:
XYintx = [Xzx; repmat(Yd(1,1),1,length(Xzx))];
The x values are in the first row, the y values in the second. Call it whatever you like. (The rows of the matrix are the arguments of the plot function in this and the code I provided for your earlier Question.)
Vaultec
2014 年 7 月 2 日
Ok thank you very much
Star Strider
2014 年 7 月 2 日
My pleasure!
Edwin
2015 年 2 月 24 日
Hi,
This is a very great thread! Thank you for your elucidation on finding intersections on the current gca. I've been trying to understand the code better but I've seem to run into a bit of an issue. IDK if this is the correct analysis but this code seems to not work if there are an odd number of intersection points. Whenever there are even number of intersection points, it does seem to work. I thought a remedy to this would be to try to add an arbitrary line to increase the number of intersections to an even number but the added line seems to create another error.
In essence, to the original problem; whenever there are an odd number of intersections, I get the following error:
Attempted to access Ydn(0); index must be a positive integer or logical.
This seems to arise because Zxi has a value of '1' in its array. Again, I'm still trying to figure out exactly how the code works to find the intersection. But Any ideas or suggestions would be greatly appreciated. Thank you for your help!
Cheers
Star Strider
2015 年 3 月 22 日
The problem is not the code, but that you are attempting to reference ‘Ydn(0)’. Zero is not a positive integer. Arrays in MATLAB are only allowed to be positive integers: (1, 2, ...).
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Axis Labels についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
