Hold plot after each push button press GUI
Show older comments
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 Comments
Adam
on 13 Dec 2016
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
on 13 Dec 2016
Geoff Hayes
on 13 Dec 2016
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
on 13 Dec 2016
Edited: Tyler Murray
on 13 Dec 2016
Tyler Murray
on 13 Dec 2016
Adam
on 13 Dec 2016
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
on 13 Dec 2016
Geoff Hayes
on 13 Dec 2016
Why are you drawing/plotting to a figure rather than adding an axes to your GUI (and plotting the data there)?
Tyler Murray
on 13 Dec 2016
Tyler Murray
on 14 Dec 2016
Answers (0)
Categories
Find more on Interactive Control and Callbacks 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!