Hold plot after each push button press GUI

I am trying to implement a "hold on" feature in my gui where the user can select and set of data and plot it on the same graph as the previous data. I saw one forum on here that helped but I am running into an issue. The code is:
ah = findobj(0,'Type','axes','Tag','MyPlotAxes');
if isempty(ah)
fh = figure('Name','Simulation','NumberTitle','off');
ah = axes('Parent',fh, 'Tag', 'MyPlotAxes');
hold(ah, 'on');
end
plot(ah, x, y);
The two sets of data do plot on one graph, however the axes of the figure becomes pasted as the background of the GUI. I have tried redefining the axes specific to each plot but have had no luck.

10 件のコメント

Adam
Adam 2016 年 12 月 13 日
I'm not entirely sure I understand your problem. It sounds like a symptom of not specifying an axes when plotting, but here you are specifying a specific axes.
This is a very convoluted way to get hold of or find an axes handle though. You would be much better off just keeping hold of the axes handle when you create it than having to do a findobj on it. For a start there is no guarantee you won't have two axes existing with that same tag since you are searching from the root node.
Also you are only using
hold( ah, 'on' )
if you had to recreate the axes rather than if it already existed. Is this because it should always be guaranteed to have hold set to on if it already exists?
Tyler Murray
Tyler Murray 2016 年 12 月 13 日
My problem is that my plots are generated in a callback function. Each time the push button is pressed to select new data, the callback function wants to create a new figure. The hold(ah,'on') was part of the solution I found in another forum. I have tried my own different combinations but haven't had any success. If you have any suggestions I'd love to hear them.
Geoff Hayes
Geoff Hayes 2016 年 12 月 13 日
Tyler - you may need to provide a small example of your code that we can run which exhibits this behaviour. Presumably you are programmatically creating a GUI (rather than using GUIDE). Is this the case? If I copy and paste your code into a test app, then each time I press the button the axes is updated and the GUI (figure) remains the same.
function testGui
hGui = figure;
hButton = uicontrol(hGui,'Style','pushbutton','String','Press Me','Position',[40, 80, 120, 40], 'Callback',@buttonCallback);
offset = 0;
function buttonCallback(~, ~)
offset = offset + 1;
hAxes = findobj(0,'Type','axes','Tag','MyPlotAxes');
if isempty(hAxes)
hFig = figure('Name','Simulation','NumberTitle','off');
hAxes = axes('Parent',hFig, 'Tag', 'MyPlotAxes');
hold(hAxes, 'on');
end
x = linspace(-2*pi,2*pi,1000);
y = sin(x) + offset;
plot(hAxes, x, y);
end
end
Alternatively, rather than using findobj to find the handle to the axes, just declare it in the workspace of the testGui function which is visible to its nested functions (including the callback)
function testGui
hGui = figure;
hButton = uicontrol(hGui,'Style','pushbutton','String','Press Me','Position',[40, 80, 120, 40], 'Callback',@buttonCallback);
offset = 0;
hAxes = [];
function buttonCallback(~, ~)
offset = offset + 1;
if isempty(hAxes)
hFig = figure('Name','Simulation','NumberTitle','off');
hAxes = axes('Parent',hFig, 'Tag', 'MyPlotAxes');
hold(hAxes, 'on');
end
x = linspace(-2*pi,2*pi,1000);
y = sin(x) + offset;
plot(hAxes, x, y);
end
end
Tyler Murray
Tyler Murray 2016 年 12 月 13 日
編集済み: Tyler Murray 2016 年 12 月 13 日
Geoff, Thank you for your reply. I am using GUIDE. I will put a more detailed explanation below; my code is as follows.
function pushbutton1_Callback(~, ~, handles)
x = randi([0 150],1,5);
y = randi([0 150],1,5);
if check == 1 && holdPlot == 0
figure('Name','Simulation','NumberTitle','off')
plot(x, y)
grid on;
elseif check == 1 && holdPlot == 1
hAxes = findobj(0,'Type','axes','Tag','MyPlotAxes');
if isempty(hAxes)
hfig = figure('Name','Simulation','NumberTitle','off');
hAxes = axes('Parent',hfig, 'Tag', 'MyPlotAxes');
hold(hAxes, 'on');
end
end
plot(hAxes, x, y);
The GUI I have has several check boxes that indicates what data to plot. There is a separate check box that says hold plot where data from the same check box will continuously be plotted against each other. So as the elseif states, if check (that specific data of interest) and holdPlot is checked, keep plotting them against each other. This is not the main function of the GUI which is why it is an elseif. But all of this happens within the pushbutton callback function.
I tried to define my axis hAxes = [] as you suggested but they plot did not "hold on", two separate figures were created. Thank you in advance for your help.
Tyler Murray
Tyler Murray 2016 年 12 月 13 日
So using
hAxes = findobj(0,'Type','axes','Tag','MyPlotAxes');
the gui does hold on the plot, the problem being the background of the actual gui is replaced. But using
hAxes = [];
2 separate figures are generated. Which I assume means that it didn't find the same axes handle to write to each time the program went to plot?
Adam
Adam 2016 年 12 月 13 日
check and holdPlot are both undefined in that function.
Also you explicitly create a new figure and then also have a plot instruction that does not use any explicit axes. This is always a bad idea.
Tyler Murray
Tyler Murray 2016 年 12 月 13 日
You're right I forgot to copy part of the code in there underneath the function.
check = get(handles.check1,'Value');
holdPlot = get(handles.holdOn, 'Value');
Can you give me a better suggestion on how to plot it Adam?
Geoff Hayes
Geoff Hayes 2016 年 12 月 13 日
Why are you drawing/plotting to a figure rather than adding an axes to your GUI (and plotting the data there)?
Tyler Murray
Tyler Murray 2016 年 12 月 13 日
This is my first time working with a GUI and I don't know how to do it any other way haha
Tyler Murray
Tyler Murray 2016 年 12 月 14 日
I'm all set I believe. It was because I wasn't specifying xlabel(axis, 'Words'). Thanks for everyone's help.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeInteractive Control and Callbacks についてさらに検索

質問済み:

2016 年 12 月 13 日

コメント済み:

2016 年 12 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by