Adding a Graph into a uipanel GUI
49 views (last 30 days)
Show older comments
I am trying to add a graph into a uipanel that I have on my matlab GUI. I have a pushbotton that intakes information from empty text boxes and then after I use the push botton it makes a graph. I want to have this graph appear on my uipanel rather than a second window, is that possible or would I have to use a axis? I was planning on having adding a map into that panel as well, but later on.
0 Comments
Answers (1)
Thomas Satterly
on 6 Dec 2019
You need to create an axis object where the data will be plotted and pass this axis as the first argument to your plot function.
% Create a figure
fh = figure();
% Create a panel to hold the plot axis
axisPanel = uipanel(fh, 'Position', [0 0 0.5 1], 'BackgroundColor', [1 1 1]);
% Create a new axis on the panel
leftAxis = axes(axisPanel, 'Position', [0.1 0.1 0.8 0.8]);
% Create a different panel to hold the button and a second axis
buttonPanel = uipanel(fh, 'Position', [0.5 0 0.5 1], 'BackgroundColor', [0.7 0.7 0.7]);
% Create an axis on the right panel
rightAxis = axes(buttonPanel, 'Position', [0.1 .3 0.8 0.6]);
% Create pushbuttons to plot data
leftPlotButton = uicontrol(buttonPanel, 'Style', 'pushbutton', ...
'String', 'Plot Left', 'Units', 'normalized', 'Position', [0.1 0 0.3 0.1], ...
'Callback', @(src, event) buttonPressCallback(leftAxis));
rightPlotButton = uicontrol(buttonPanel, 'Style', 'pushbutton', ...
'String', 'Plot Right', 'Units', 'normalized', 'Position', [0.6 0 0.3 0.1], ...
'Callback', @(src, event) buttonPressCallback(rightAxis));
function buttonPressCallback(thisAxis)
% Plot on the specified axis
plot(thisAxis, rand(1, 10), rand(1, 10), 'r.');
end
It's also possible to create the graphics object itself (e.g. 'matlab.graphics.chart.primitive.Line') and set the 'Parent' property to the axis you want it to appear on, rather than use a 'plot'-style command
See Also
Categories
Find more on Annotations 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!