MATLAB Answers

Adding a Graph into a uipanel GUI

38 views (last 30 days)
Erick Magana
Erick Magana on 6 Dec 2019
Commented: Erick Magana on 6 Dec 2019
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

Sign in to comment.

Answers (1)

Thomas Satterly
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

  1 Comment

Erick Magana
Erick Magana on 6 Dec 2019
Sorry I should have been a bit more specific, I am getting user data from a text box then after have then push the oushbutton. That pushbutton will do some calculations and use the data from the calculation to display the plot. Would it require me to have mulitple pushbuttons to do this, or is one enough?
These are all different functions within my actual code. The math and plotting code is all done under the pushbutton function.

Sign in to comment.

Sign in to answer this question.