Add lines to plot

20 views (last 30 days)
Max Müller
Max Müller on 7 Aug 2014
Edited: Max Müller on 8 Aug 2014
Hey Guys, I have created a func. which plots me some Data. Inside my func. I use this code
Plot = figure
set(Plot, 'Position', [306,200, 1610,1085]);
set(Plot,'name','Plot','numbertitle','off');
my Question is now: how can I add more Plot lines without opening a new figure ?

Accepted Answer

Max Müller
Max Müller on 7 Aug 2014
Edited: Max Müller on 8 Aug 2014
if isempty( findobj('Tag','Plot')) == 1
Plot = figure
set(Plot, 'Position', [306,200, 1610,1085]);
set(Plot,'name','Plot','Tag','Plot','numbertitle','off');
else
MakeFigureCurrent = findobj('Tag','Plot')
figure(MakeFigureCurrent)
hold on
end
Some day I ll be a MatLabGOD...but not in this life

More Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 7 Aug 2014
Use hold on
  4 Comments
Max Müller
Max Müller on 7 Aug 2014
The problem is: hold on doesnt work as long as i have the Plot=figure command. However i need the the Plot = figure command. Otherwise, matlab displays the plot inside my GUI.
Adam
Adam on 7 Aug 2014
Plot = figure;
hAxes = gca;
hold on
plot( hAxes,... )
allows you to get the axes handle from the first new figure you open and use this to plot on next time

Sign in to comment.


Geoff Hayes
Geoff Hayes on 7 Aug 2014
Max - consider renaming your figure handle from Plot to (say) plotFigHandle or something that is a little different from the MATLAB built-in function plot.
Once you have create the figure, then try grabbing the current axes from that figure and setting the hold property to on
plotFigureHandle = figure;
set(plotFigureHandle, 'Position', [306,200, 1610,1085]);
set(plotFigureHandle,'name','Plot','numbertitle','off');
plotAxesHandle = gca;
hold(plotAxesHandle,'on');
And then you can plot multiple plot lines to the figure using the plotAxesHandle
x=-2*pi:0.0001:2*pi;
y=sin(x);
plot(plotAxesHandle,x,y,'b')
plot(plotAxesHandle,x,y+0.5,'r')
Try the above and see what happens!
  4 Comments
Geoff Hayes
Geoff Hayes on 7 Aug 2014
Max - what you wanted to do wasn't very clear from your question which was how can I add more Plot lines without opening a new figure?
The alternative to assigning a Tag value, is to just save the plotFigureHandle and/or plotAxesHandle to the handles structure and use guidata. Presumably you are using GUIDE, and within a callback you are trying to create the figure and so one of the inputs to this callback is handles
function someWidget_Callback(hObject, eventdata, handles)
% if the plot figure handles is not a field of handles, then
% create it
if ~isfield(handles.plotFigureHandle)
plotFigureHandle = figure;
set(plotFigureHandle, 'Position', [306,200, 1610,1085]);
set(plotFigureHandle,'name','Plot','numbertitle','off');
plotAxesHandle = gca;
hold(plotAxesHandle,'on');
handles.plotFigureHandle = plotFigureHandle;
handles.plotAxesHandle = plotAxesHandle;
guidata(hObject,handles);
end
Adam
Adam on 7 Aug 2014
Edited: Adam on 7 Aug 2014
If you just want to bring a previous axes handle to be the current one you can type:
axes( plotAxesHandle )
if you want to return to the same axes some time later on when you can't assume that gca points to the correct axes.
Personally I don't like doing this and prefer to pass the axes handle to the plot function as this just plots in the correct place instead of bringing the figure containing the axes to the foreground (which I don't always want) which is what the command above does.
Remember though that the plot function associates to an axes handle, not explicitly to a figure handle so just keeping the figure handle is not enough.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!