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
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.
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.
Dear Star Thanks for the detailed explanation. Really appreciate it.
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
