Clear Filters
Clear Filters

Segment of Graph Extraction

2 views (last 30 days)
Daniel
Daniel on 17 Jun 2011
Can you use any plotting tools to extract a segment of a graph and replot that segment into a new figure or subplot?

Accepted Answer

Walter Roberson
Walter Roberson on 17 Jun 2011
How complex is your scene? The work needed for a patch or surface object is more than the work needed for line graphs.
Gerd's approach has the property of not drawing portions of the graph that would extend beyond the edges. For example if you defined a plot as a straight line between two points and you want to display the middle of that line with the end-points both outside of the window, Gerd's approach would not draw the line at all.
If you have text written on the graph that would be partly inside the sub-area, do you want the fraction of the text displayed?
The easiest approach might be to copyobj() the children of the axes to the new axes, and then set the XLim and YLim properties of the new axes to show only the portion you want.
  1 Comment
Gerd
Gerd on 17 Jun 2011
Walter is absolutely right, I didn't think about such a scenario :-)

Sign in to comment.

More Answers (1)

Gerd
Gerd on 17 Jun 2011
Hi Daniel,
I don't know any plotting tool in Matlab but with some lines of code it shouldn't be a problem.
xlimit=get(gca,'XLim');
nearest= min(abs(a-xlimit(1)));
% find index of nearest time value
indexX1 = find(a==xlimit(1)+nearest | a==xlimit(1)-nearest);
nearest= min(abs(a-xlimit(2)));
% find index of nearest time value
indexX2 = find(a==xlimit(2)+nearest | a==xlimit(2)-nearest);
figure;
plot(a(indexX1:indexX2),b(indexX1:indexX2));
First, I would check the XLimits of the current axes and determine the nearest point in the time vector. Then plotting with the new indices. Of course you can also plot in a subplot.
Gerd

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!