You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Hold plot after each push button press GUI
1 view (last 30 days)
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
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
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
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
on 13 Dec 2016
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?
Tyler Murray
on 13 Dec 2016
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
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
This is my first time working with a GUI and I don't know how to do it any other way haha
Tyler Murray
on 14 Dec 2016
I'm all set I believe. It was because I wasn't specifying xlabel(axis, 'Words'). Thanks for everyone's help.
Answers (0)
See Also
Categories
Find more on Specifying Target for Graphics Output 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)