MATLAB Answers

Link 2 plots with different variables

26 views (last 30 days)
Rajesh Rajaram
Rajesh Rajaram on 5 Sep 2014
Answered: Anish Mitra on 29 Sep 2014
Here is an example of what I want to do. Let's say I have 5 variables in my workspace A, B, C, D & E with the same length. Now I perform the following operation:
figure(1); plot(A(e1),B(e1),'kx',A(e2),B(e2),'ro')
figure(2); plot(C(e1),D(e1),'kx',C(e2),D(e2),'ro')
So far it is pretty rudimentary. But what I want to do is I want to zoom into figure 1 and I want figure 2 to plot only those points that are there now in figure 1. And I should be able to do it vice versa.
If I want to use linkaxes function, then I should plot 4 plots to make this operation work. That is ok if I only want to do for only 4 variables. Or I could change my find operation and refresh the figures. It is not a bad idea. But I was wondering if there's an elegant way to do this operation.
Thank you all in advance
  1 Comment
dpb on 5 Sep 2014
I don't see the why of four figures but iiuc the operation you're asking for isn't really linking axes since the scales of the two aren't necessarily commensurate. It seems the requirement is that you have to maintain the same subset of the two sets of indices and refresh as you say; can't think of a plotting property that is linkable that has the proper information.

Sign in to comment.

Answers (1)

Anish Mitra
Anish Mitra on 29 Sep 2014
As I understand, your objective is to link the data contained in two different axes such that when you zoom into one axis/figure, the visible data after zooming in will be plotted on the other axis/figure. This can definitely be done in MATLAB using callback functions and axes and figure handles .
Setting the Callback property of a Graphics object allows the specified function to be called when the appropriate action takes place. To learn more about Graphics objects and their Callback properties in MATLAB, refer to the following links.
For the question in concern here, you will need to implement the following steps after generating the original figures.
1. Create the zoom handle of the figure.
>> z1 = zoom(hFigure1)
% 'hFigure1' contains the handle of figure 1
2. Set the ‘ActionPostCallback’ property of 'z1' to a custom defined MATLAB function – 'AfterZoom' . This will execute the function after a zoom operation on the figure finishes.
>> set(z1,'ActionPostCallback',{@AfterZoom, hFigure2});
% hFigure2 contains the handle of figure 2
3. You will now need to create the custom function and save it in a MATLAB file, ‘AfterZoom.m’. The input arguments are the current object (current figure handle), EventData and the handle of the other figure. There are no output arguments.
In this function you will need to access the ‘axes’ and 'line' objects using the ‘Children’ field of the ‘figure’ and ‘axes’ objects respectively. From the objects find out the axis limits ( XLim , YLim ) and the data ( XData , YData ) . Use the ‘get’ command to access the required fields. Following is a sample structure of the function file ‘AfterZoom.m’ :
function AfterZoom (hObject, EventData, hOtherFigure)
hAxes = get(hObject, 'Children');
hLine = get(hAxes, 'Children');
xLimits = get(hAxes,'XLim');
yLimits = get(hAxes,'YLim');
xData = get(hLine,'XData');
yData = get(hLine,'YData');
. %Extract the required points and plot on other figure%
Once you have the data for the axis limits and the plotted points in the specific figure, you should be able to extract the X and Y location of the points within the axis limits and plot them on the other figure using the handle, ‘hOtherFigure’ .
You can use the following resources for examples on setting Callback properties for ‘line’ and ‘axes’ objects. I have personally found the videos very informative and easy to learn from.
There are also numerous other MATLAB Central videos related MATLAB Graphics on that you can take advantage of.



Community Treasure Hunt

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

Start Hunting!