How to effectively plot semi continuous graph
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Dear Matlab Code The objective was to have a plot as below

At the moment, I am using a rather ineffecient approach, such as,
Xaxis = [273.5 275.5 277.5 279.5 281.5 283.5 303.5 305.5 307.5 315.5 317.5 319.5];
Yaxis = [0.61202 1.62647 1.37831 2.74613 2.24585 0.49887 0.61202 0.73511 2.2399 1.62647 1.99175 4.61222];
subplot(2,1,1);
plot (Xaxis (1,1:6), Yaxis (1,1:6), '--mo');
hold on
plot (Xaxis (1,7:9), Yaxis (1,7:9), '--mo');
hold on
plot (Xaxis (1,10:12), Yaxis (1,10:12), '--mo');
May I know any other smart alternative to the above line?
Thanks in advance for the time.
採用された回答
Star Strider
2017 年 7 月 6 日
I cannot say this is better, but it is different, and does the vector splitting on its own:
Xaxis = [273.5 275.5 277.5 279.5 281.5 283.5 303.5 305.5 307.5 315.5 317.5 319.5];
Yaxis = [0.61202 1.62647 1.37831 2.74613 2.24585 0.49887 0.61202 0.73511 2.2399 1.62647 1.99175 4.61222];
Xsplit = diff([0 find(diff(Xaxis)>2) length(Xaxis)]); % Find Discontinuities & Create Lengths Vector
Xc = mat2cell(Xaxis, 1, Xsplit); % Split ‘Xaxis’
Yc = mat2cell(Yaxis, 1, Xsplit); % Split ‘Yaxis’
figure(1)
plot(Xc{1}, Yc{1}, '--mo', Xc{2}, Yc{2}, '--mo', Xc{3}, Yc{3}, '--mo')
6 件のコメント
balandong
2017 年 7 月 6 日
Hi Star, I really appreciate the effort and creativity you have with the suggested code. However, I am having difficulties using your technique to generalize to other time series. For example, If I have the following data points
Xaxis = [273.5 275.5 277.5 279.5 281.5 283.5 303.5 305.5 307.5 315.5 ...
317.5 319.5 327.5 329.5 331.5 339.5 341.5 343.5 351.5 353.5 ...
355.5 363.5 365.5 367.5 375.5 377.5 379.5 387.5 389.5 391.5];
Yaxis = [0.61202 1.62647 1.37831 2.74613 2.24585 0.49887 0.61202 0.73511 2.2399 ...
1.62647 1.99175 4.61222 1.13016 0.98326 0.75297 1.50735 1.73367 4.35216 ...
0.62393 1.24332 1.23141 1.87462 0.74702 4.74126 0.87209 1.99175 0.99517 ....
0.87209 1.36243 4.62414 ];
I expect the graph to be like this

Any how, I am amazed on how you split the vector automatically.
My pleasure.
This works with your new vectors, without any other modification to my code:
Xaxis = [273.5 275.5 277.5 279.5 281.5 283.5 303.5 305.5 307.5 315.5 ...
317.5 319.5 327.5 329.5 331.5 339.5 341.5 343.5 351.5 353.5 ...
355.5 363.5 365.5 367.5 375.5 377.5 379.5 387.5 389.5 391.5];
Yaxis = [0.61202 1.62647 1.37831 2.74613 2.24585 0.49887 0.61202 0.73511 2.2399 ...
1.62647 1.99175 4.61222 1.13016 0.98326 0.75297 1.50735 1.73367 4.35216 ...
0.62393 1.24332 1.23141 1.87462 0.74702 4.74126 0.87209 1.99175 0.99517 ....
0.87209 1.36243 4.62414];
Xsplit = diff([0 find(diff(Xaxis)>2) length(Xaxis)]); % Find Discontinuities & Create Lengths Vector
Xc = mat2cell(Xaxis, 1, Xsplit); % Split ‘Xaxis’
Yc = mat2cell(Yaxis, 1, Xsplit); % Split ‘Yaxis’
figure(1)
plot(Xc{1}, Yc{1}, '--mo')
hold on
for k1 = 2 : length(Xc)
plot(Xc{k1}, Yc{k1}, '--mo')
end
hold off
The only addition was to add the loop to plot the individual segments, since there are now nine of them. The vectors split automatically. The plot is the same as you posted, so I won’t re-post it here.
balandong
2017 年 7 月 7 日
Hi Star, Thanks for the updated code.You deserve more than 1 vote from me. By the way, may I know how split the vector automatically?. In this case, it can automatically split into the 6 3 3 3 3 3 3 3. Run the code line by line, I guest the splitting happen at
Xsplit = diff([0 find(diff(Xaxis)>2) length(Xaxis)]);
However, I still cannot comprehend completely, how it work. Appreciate, if you can explain more.
As always, my pleasure.
The splitting occurs with these three assignments and function calls:
Xsplit = diff([0 find(diff(Xaxis)>2) length(Xaxis)]); % Find Discontinuities & Create Lengths Vector
Xc = mat2cell(Xaxis, 1, Xsplit); % Split ‘Xaxis’
Yc = mat2cell(Yaxis, 1, Xsplit); % Split ‘Yaxis’
The first, ‘Xsplit’, takes advantage of the fact that your ‘continous’ values are 2 values apart, and the discontinuities are more than that. It then takes the difference of the vector created by the find call that detects these differences and returns the indices, padded by 0 on the low end and ‘length(Xaxis)’ on the high end, to create the lengths of the individual segments. This is necessary because the mat2cell function will split the vectors in its first argument by these dimensions. The easiest way to see how it works is the reverse of the way I constructed it, so in order, explore these:
diff(Xaxis)
diff(Xaxis)>2
find(diff(Xaxis)>2)
[0 find(diff(Xaxis)>2) length(Xaxis)]
Xsplit
These will demonstrate how the ‘Xsplit’ assignment works.
The mat2cell calls take the vectors in their first arguments and split them (in my code here) according to the the second and third arguments. The second and third arguments have to equal the row and column dimensions respectively of the first argument. Since the first arguments are row vectors, the second argument is 1 (the row size) and the third argument is the ‘Xsplit’ vector, the sum of the elements of which are the column size. It splits the first argument according to the second and third arguments.
The documentation on mat2cell explains its operation much better than I can, so reading that documentation will provide an illustration of its capabilities, and also help to illustrate how my code works. The mat2cell function returns cell arrays as its output.
To understand the cell array references I used in the plot calls, see the documentation on Cell Arrays (link). Cell arrays are extremely useful, and have their own properties that are necessary to understand in order to use them effectively.
If you have any further questions on how my code works, I will do my best to provide useful and appropriate explanations.
balandong
2017 年 7 月 7 日
Dear Star Thanks for the detailed explanation. Really appreciate it.
Star Strider
2017 年 7 月 7 日
As always, my pleasure.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Creating, Deleting, and Querying Graphics Objects についてさらに検索
参考
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)
