MATLAB: Create figure2 from point selected in figure1 until closing figure1

figureplotting

Hi,
I am working with a pair of figures that I would like to make more interactive. I have two vectors: v1, v2.
  • In Figure 1, I plot points from v1.
  • Then I select an observation in Figure 1, i_obs.
  • A Figure 2 is created with a bar indicating the value v2(i_obs).
The point is that I would like to keep on clicking points in Figure 1 and creating the subsequent Figures 2 until I close Figure 1. By the moment what I have is the following, but I cannot keep Figure 1 open in order to clik and re-draw Figure 2 (it's just a single-use pair of Figures). So…any help please?
Thank you so much in advance 🙂
plot(v1)
datacursormode on
waitforbuttonpress
dcm_obj = datacursormode(gcf);
dc_info = getCursorInfo(dcm_obj);
figure, bar(v2(dc_info.DataIndex))

Best Answer

Hi,
I guess you can use 'subplot' method available in MATLAB, if you do not want Figure 2(which is generated after a point is selected on Figure 1) to not last after next click on Figure 1 is made.
For 'subplot' doc:
subplot(2,1,1);
plot(v1);
datacursormode on;
waitforbuttonpress;
dcm_obj = datacursormode(gcf);
dc_info = getCursorInfo(dcm_obj);
subplot(2,1,2);
bar(v2(dc_info.DataIndex));
I guess inorder to make it more interactive, you can make use of click callbacks. Refer the following link:
x = 1:10;
y = [1 4 9 16 25 36 49 64 81 100];
f = subplot(2,1,1);
title('Sub Plot 1');
plot(x)
set(gcf,'WindowButtonDownFcn',@mytestcallback)
function mytestcallback(src,~)
pt = get(gca,'CurrentPoint');
subplot(2,1,2);
%Code to plot your graph
end
Hope this helps!