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 Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by