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.

 Accepted Answer

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 Comments

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.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!