How to use subplot in a UIFIGURE?

85 次查看(过去 30 天)
JuanMa
JuanMa 2022-11-16
评论: JuanMa 2022-11-17
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

回答(2 个)

Les Beckham
Les Beckham 2022-11-16
编辑:Les Beckham 2022-11-16
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 2022-11-16
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 个评论
Voss
Voss 2022-11-17
"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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by