How to use subplot in a UIFIGURE?

166 views (last 30 days)
JuanMa
JuanMa on 16 Nov 2022
Commented: JuanMa on 17 Nov 2022
Dear All,
I am working on a code that calls a function to orderly display different plots in a UIFIGURE. I am not able to plot 2 signals against time on two different axes using the subplot command.
In a LiveScript script I use this command, subplot, correctly but I have no experience embedding this type of plot in a UIFIGURE.
I am guiding myself with the help of MATLAB and the web but I did not find an example that can help me.
I am attaching the code, with errors, of the function for displaying the data that I need to analyze.
Grateful if you can guide me in solving the errors that I am making.
Thanks and regards
JuanMa
function pantalla_plots(v_time, v_t1, v_t2, v_t, v_debug_v_1)
% pantalla_plots(time, t1_vect, t2_vect, t_vect, debug_var_1_vect);
fig = uifigure('Name','Run logic Oil_Temp - Plots - Ingersoll','Position',[1 100 1280 900]);
p1 = uipanel(fig,'Position',[15 10 600 410]);
ax = uiaxes(p1,'Position',[10 10 550 390]);
p2 = uipanel(fig,'Position',[15 450 600 410]);
bx = uiaxes(p2,'Position',[10 10 550 390]);
fig.Color = '#D2E1F0';
plot(ax, v_time, v_t1, v_time, v_t2);
ax.XGrid = 'on';
ax.YGrid = 'on';
ax.Title.String = 't1 & t2';
%p2.AutoResizeChildren = 'off';
bx1 = subplot(2, 1, 1, 'Parent', p2);
plot(bx1, v_time, v_t);
bx2 = subplot(2, 1, 2, 'Parent', p2);
plot(bx2, v_time, v_debug_v_1);
%bx.XGrid = 'on';
%bx.YGrid = 'on';
end

Answers (2)

Les Beckham
Les Beckham on 16 Nov 2022
Edited: Les Beckham on 16 Nov 2022
When you post a question that states that you are getting error(s), you should post the complete text of the error message (everything in red).
It looks like you are almost there, though.
This slightly corrected code seems to work for me:
fig = uifigure('Name','Run logic Oil_Temp - Plots - Ingersoll','Position',[1 100 1280 900]);
p1 = uipanel(fig,'Position',[15 10 600 410]);
ax = uiaxes(p1,'Position',[10 10 550 390]);
p2 = uipanel(fig,'Position',[15 450 600 410]);
bx = uiaxes(p2,'Position',[10 10 550 390]);
fig.Color = '#D2E1F0';
ax.XGrid = 'on';
ax.YGrid = 'on';
ax.Title.String = 't1 & t2';
plot(ax, [0:10], [0:10].^2); % <<< made up data to plot; replace with your original plot call
p2.AutoResizeChildren = 'off';
bx1 = subplot(2, 1, 1, 'Parent', p2); % <<< restored this; you need it
plot(bx1, [0:100], [0:100].^3); % <<< made up data to plot; replace with your original plot call
bx2 = subplot(2, 1, 2, 'Parent', p2);
plot(bx2, [0:0.01:4*pi], sin([0:0.01:4*pi])); % <<< made up data to plot; replace with your original plot call
Results in the below (resized to post here). I ran this on my local machine as uifigure is not supported on Answers.

Voss
Voss on 16 Nov 2022
At first I get an error:
Error using subplot
Adding subplots to a container with the 'AutoResizeChildren' property set to 'on' is not supported.
Error in pantalla_plots (line 18)
bx1 = subplot(2, 1, 1, 'Parent', p2);
so I uncomment the line where you set p2.AutoResizeChildren = 'off'.
Then the code runs without error and produces two panels, one with a single axes and one with 2 subplots.
Is that not correct?
The image above was produced using:
pantalla_plots(0:10,rand(1,11),10+rand(1,11),20+rand(1,11),30+rand(1,11))
  3 Comments
Voss
Voss on 17 Nov 2022
"Could it be that the subplot command in a UIFIGURE is not enabled in MATLAB r2019b?"
It could be, yes. I tested creating a subplot in a uifigure in R2022a, which worked, and in R2017b, which did not work. So somewhere in between those two releases is where support was added. I don't know how to find documentation for older releases that I don't own, so I can't check R2019b specifically.
Maybe look into using tiledlayout, which was introduced in R2019b.

Sign in to comment.

Categories

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!