How can I swith between plots in the same plot window?

19 vues (au cours des 30 derniers jours)
Madina Makhmutova
Madina Makhmutova le 24 Mar 2018
I have a time-lapse matrix. Each column is a region of interest (Roi) and each row is the time point value. I need to quickly visualize each trace individually. Is there a way to plot all the traces at the same time but have only one at a time selected for visualization so I can move quickly through the traces just by clicking the up/down buttons to move through selections?

Réponse acceptée

Rik
Rik le 24 Mar 2018
Modifié(e) : Rik le 25 Mar 2018
You can plot all the lines and toggle the visibility with the KeyPressFcn of the figure, which sets the Visible property of the line objects that plot returns.
Edit: see an example below
f=figure(1);clf(1)
h=plot(rand(4,2));axis([1 3 0 1])
handles=struct('f',f,'h',h);
handles.visible_index=1;%keep track of which plot is selected
set(h,'Visible','off');
set(h(handles.visible_index),'Visible','on');
guidata(f,handles)
set(f,'KeyPressFcn',@SetVisibility)
function SetVisibility(hObject,eventdata)
switch eventdata.Key
case {'uparrow','up'}
direction=1;
case {'downarrow','down'}
direction=-1;
otherwise
return
end
handles=guidata(hObject);
%set the old plot to invisible
set(handles.h(handles.visible_index),'Visible','off');
%find the new index to be set
handles.visible_index=mod(handles.visible_index+direction,length(handles.h));
if handles.visible_index==0,handles.visible_index=length(handles.h);end
%set the new plot to visible
set(handles.h(handles.visible_index),'Visible','on');
%optional: set the title to the index value
title(sprintf('selected plot: %d',handles.visible_index))
guidata(hObject,handles)
end
  4 commentaires
Rik
Rik le 20 Avr 2018

Just as a note for future reference (in response to your email): I am receiving notifications, although I did choose to disable email notifications. That way I can keep track of the questions that get updates, but only when I have time to actually reply.

The code below is the adapted version. If you make sure h is a matrix where each row should be displayed simultaneously, this will do that for you. Don't forget to explicitly code a range for the axes (like I did with axis([1 4 0 1]) below). This will prevent the axes to jump around when you hide plots.

f=figure(1);clf(1)
y1=rand(4,2);
y2=rand(4,2);
y3=rand(4,2);
x=1:4;
h=plot(x,y1,'y',x,y2,'k',x,y3,'r');
h=reshape(h,2,3);
axis([1 4 0 1])
handles=struct('f',f,'h',h);
handles.visible_index=1;%keep track of which plot is selected
set(h,'Visible','off');
set(h(handles.visible_index,:),'Visible','on');
guidata(f,handles)
set(f,'KeyPressFcn',@SetVisibility)
function SetVisibility(hObject,eventdata)
switch eventdata.Key
    case {'uparrow','up'}
        direction=1;
    case {'downarrow','down'}
        direction=-1;
    otherwise
        return
end
handles=guidata(hObject);
%set the old plot to invisible
set(handles.h(handles.visible_index,:),'Visible','off');
%find the new index to be set
handles.visible_index=mod(handles.visible_index+direction,size(handles.h,1));
if handles.visible_index==0,handles.visible_index=size(handles.h,1);end
%set the new plot to visible
set(handles.h(handles.visible_index,:),'Visible','on');
%optional: set the title to the index value
title(sprintf('selected plot: %d',handles.visible_index))
guidata(hObject,handles)
end
Madina Makhmutova
Madina Makhmutova le 20 Avr 2018
Dear Rik,thank you very much! This works nicely!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by