MATLAB: Continuous Linking of 2 Plots

automaticallybuttondownfunctionguilinkingMATLABplotupdate

Hi all,
I have a question concerning "linked" plots:
I have two (sub?) plots. The first one contains data of several variables versus time t with t e [t1,t2]. The second plot displays spectra (absorbance versus wavelength). Each spectrum is specific for a certain point in time tx where again tx e [t1,t2].
My idea now is to link the plots in a way that the time axis position of the mouse pointer in the first plot is processed and used to display the corresponding spectrum at that point of time in the second plot. Interpolation between data points is not necessary.
Do you know an feasible approach to tackle this problem (possibly even an example?)?
Thanks in advance,
bearli

Best Answer

  • Here's a small GUI, the main part is ln_button and in particular you have to customize what happens at its end, I just plot some random numbers.
    keyPressFcn and moveArrow are just there to make it fancy, you don't need to click the graph but you can browse the graph with arrow keys.
    Also, you could speed it up by creating a line object s.ln(3) in the first axis, assign it a dummy value and then just substitute the plot with a set(s.ln(3),'Ydata',...,'Xdata',...).
    function myGUI
    % Figure
    s.fh = figure('Units','pixels','Pos',[200 100 800 600],...
    'Numbert','off','Menubar','none','KeyPress',@keyPressFcn);
    % Two axes
    s.a(1) = axes('Units','pixels','Pos',[60 50 680 210]);
    s.a(2) = axes('Units','pixels','Pos',[60 350 680 210],'Xlim',[1,100]);
    % Line object in first axis
    s.ln(1) = line(1:100,(.1:.1:10) + rand(1,100),...
    'lines','none','marker','.','markers',5,...
    'markere','k','buttond',{@ln_buttond});
    % Additional enlarged marker to highlight selected point
    s.ln(2) = line(-1,-1,'lines','none','marker','o',...
    'markere','r','markers',5);
    % ln_buttond --------------------------------------------------------------
    function ln_buttond(varargin)
    XY = get(s.ln(1),{'Xdata','Ydata'});
    % If mouse click
    if isempty(varargin{end})
    cpaxes = get(s.a(2),'currentpoint');
    idx = abs(cpaxes(1) - XY{1}) < .5;
    else
    idx = varargin{end};
    end
    % Set marker
    set(s.ln(2),'Ydata',XY{2}(idx),'Xdata',XY{1}(idx));
    % Plot on first axis (from the bottom)
    plot(s.a(1),rand(100,1),rand(100,1))
    end
    % keyPressFcn -------------------------------------------------------------
    function keyPressFcn(varargin)
    % Parse Keyboard Inputs
    switch upper(varargin{2}.Key)
    case 'LEFTARROW'
    moveArrow(-1)
    case 'RIGHTARROW'
    moveArrow(+1)
    end
    end
    % moveArrow ---------------------------------------------------------------
    function moveArrow(step)
    % Retrieve pos of red marker and maxX
    X = get(s.ln(2),'Xdata');
    X = X + step;
    % Retrieve Xlim
    Xlimits = get(s.a(2),'Xlim');
    % Crossed Xlimits
    if X >= Xlimits(1) && X <= Xlimits(2)
    ln_buttond(X)
    end
    end
    end