Add lines to plot
3 views (last 30 days)
Show older comments
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 ?
0 Comments
Accepted Answer
More Answers (2)
Azzi Abdelmalek
on 7 Aug 2014
Use hold on
4 Comments
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
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
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
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.
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!