Add lines to plot

조회 수: 12 (최근 30일)
Max Müller
Max Müller 2014년 8월 7일
편집: Max Müller 2014년 8월 8일
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 ?

채택된 답변

Max Müller
Max Müller 2014년 8월 7일
편집: Max Müller 2014년 8월 8일
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

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 8월 7일
Use hold on
  댓글 수: 4
Max Müller
Max Müller 2014년 8월 7일
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 2014년 8월 7일
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
Geoff Hayes 2014년 8월 7일
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
Geoff Hayes
Geoff Hayes 2014년 8월 7일
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 2014년 8월 7일
편집: Adam 2014년 8월 7일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by