Unable to display uicontrol within a given panel?

I figure I split my figure width in two uipanels, in the hope of having better control over my components, in a script (Soon, I hope to migrate to AppDesigner...). I tell Matlab to display a 3D plot inside the first "uipanel" (to the left) and it works perfect, after creating axes belonging to that component. But when I ask Matlab to display a checkbox uicontrol in the second uipanel, after creating a second set of axes belonging to that second panel, it does not work. Must be something conceptual I don't understand about the relatioship between axes and uipanels? I tried forcing checkbox uicontrol to be visible to no avail. Here's my code:
fig = figure;
set(fig, 'Position', [400, 400, 1500/2, 800/2]);
% Create two uipanels as separate parent containers
panel1 = uipanel(fig, 'Position', [0.1, 0.1, 0.4, 0.8], ...
"Title","First", ...
"BackgroundColor","white");
panel2 = uipanel(fig, 'Position', [0.5, 0.1, 0.4, 0.8], ...
"Title","Second", ...
"BackgroundColor","white");
% Create axes within panels
ax = axes(panel1);
% Plot your scatter data on the UI axes
scatterHandle = scatter3(ax, X, Y, Z, 'filled');
Unrecognized function or variable 'X'.
title(ax, '3D Scatter plot');
xlabel(ax, 'X');
ylabel(ax, 'Y');
zlabel(ax, 'Z');
ax2 = axes(panel2);
%ax2.Visible = 'off';
checkbox1 = uicontrol(ax2, 'Style', 'checkbox', 'String', 'Show Scatter Plot', ...
'Position', [0.1, 0.1, 0.3, 0.50], 'Callback', @toggleScatterPlot);
checkbox1.Visible = 'on';
% Store the scatter plot handle
% scatterHandle = scatter3(X, Y, Z, 'filled', 'Visible', 'off');
% Callback function for checkbox
function toggleScatterPlot(~, ~)
if get(checkbox1, 'Value')
set(scatterHandle, 'Visible', 'on'); % Show scatter plot
else
set(scatterHandle, 'Visible', 'off'); % Hide scatter plot
end
end

1 Comment

Forgot to add a screen capture:
I got an error, when running the code:
Error using uicontrol Axes cannot be a parent. Error in PANTONE_subplots (line 39) checkbox1 = uicontrol(ax2, 'Style', 'checkbox', 'String', 'Show Scatter Plot', ...
I tried changing the uicontrol line to:
checkbox1 = uicontrol(panel2, 'Style', 'checkbox', 'String', 'Show Scatter Plot', ...
'Position', [0.1, 0.1, 0.3, 0.50], 'Callback', @toggleScatterPlot);
But it still does not work? I'll continue searching the obscure reason this does not work...

Sign in to comment.

Answers (1)

"checkbox1 = uicontrol(ax2, ...)"
won't work because an axes cannot be the parent of a uicontrol, as the error message said.
"checkbox1 = uicontrol(panel2, ...)"
does work, in that it creates the checkbox in the specified panel with the specified properties.
However, you don't see it because the default Units for uicontrols created with the uicontrol() function are pixels. The Position you've used doesn't make sense if the Units are pixels. Probably you want to specify the Units as normalized (always specify Units before Position in an argument list).
X = [];
Y = [];
Z = [];
fig = figure;
set(fig, 'Position', [400, 400, 1500/2, 800/2]);
% Create two uipanels as separate parent containers
panel1 = uipanel(fig, 'Position', [0.1, 0.1, 0.4, 0.8], ...
"Title","First", ...
"BackgroundColor","white");
panel2 = uipanel(fig, 'Position', [0.5, 0.1, 0.4, 0.8], ...
"Title","Second", ...
"BackgroundColor","white");
% Create axes within panels
ax = axes(panel1);
% Plot your scatter data on the UI axes
scatterHandle = scatter3(ax, X, Y, Z, 'filled');
title(ax, '3D Scatter plot');
xlabel(ax, 'X');
ylabel(ax, 'Y');
zlabel(ax, 'Z');
ax2 = axes(panel2);
%ax2.Visible = 'off';
checkbox1 = uicontrol(panel2, 'Style', 'checkbox', 'String', 'Show Scatter Plot', ...
'Units', 'normalized', 'Position', [0.1, 0.1, 0.3, 0.50], 'Callback', @toggleScatterPlot, ...
'Visible','on');
% Store the scatter plot handle
% scatterHandle = scatter3(X, Y, Z, 'filled', 'Visible', 'off');
% Callback function for checkbox
function toggleScatterPlot(~, ~)
if get(checkbox1, 'Value')
set(scatterHandle, 'Visible', 'on'); % Show scatter plot
else
set(scatterHandle, 'Visible', 'off'); % Hide scatter plot
end
end

6 Comments

Matlab is really not for the faint of heart... In searching for solutions, I stumbled on the gridlayout manager. So I ran with this for a while, until I could not successfully use the Hold ('on') directive? Here's my new code:
fig = uifigure('Position',[300 300 800 600]);
g = uigridlayout(fig);
g.RowHeight = {22,22, 22,'1x'};
g.ColumnWidth = {150,'1x'};
checkbox1 = uicheckbox(g, 'Text', 'sRGB');
checkbox1.Layout.Row = 2;
checkbox1.Layout.Column = 1;
checkbox2 = uicheckbox(g, 'Text', 'Jaune');
checkbox2.Layout.Row = 3;
checkbox2.Layout.Column = 1;
% Axes
ax = uiaxes(g);
ax.Layout.Row = [2 4]; % S'étend sur les 4 rangées
ax.Layout.Column = 2;
% ----------------------------------------------------------------------
[~, ~, raw] = xlsread('PANTONE V4 Coated M0 de ORIS (2020 04 30).xlsx');
Lab = cell2mat(raw(:, 3:5));
Lab(end, :) = []; % Pour enlever les NaN sur la dernière rangée
% Lab à Lch, 2162 couleurs
cform = makecform('lab2lch');
Lch = applycform(Lab,cform);
% Grouping by hue angle ----------------------------------- 80 à 110
hue_angles = Lch(:, 3); % Extract hue angles
% Create a logical mask for entries within the specified range
mask = (hue_angles >= 80) & (hue_angles <= 110);
% Extract the relevant rows
Lch_80_110 = Lch(mask, :);
% revenir à Lab
L = Lch_80_110(:, 1);
C = Lch_80_110(:, 2);
h_degrees = Lch_80_110(:, 3);
% Convert hue angle from degrees to radians
h_radians = deg2rad(h_degrees);
% Calculate a* and b* components
a_star = C .* cos(h_radians);
b_star = C .* sin(h_radians);
Lab = [L, a_star, b_star];
% ---------------------------------------------------------
% Extract the numeric data from the table
CIE_L = Lab(:,1);
CIE_a = Lab(:,2);
CIE_b = Lab(:,3);
NomsCouleur = string(raw(:, 2));
cform = makecform('lab2srgb','AdaptedWhitePoint',whitepoint('D50'));
rawRGB = applycform(Lab,cform);
Diam = 20;
scatter3(ax, Lab(:, 2), Lab(:, 3), Lab(:, 1), Diam, rawRGB,'fill', 'MarkerEdgeColor', [0.5,0.5,0.5], 'Linewidth', 0.5);
xlabel(ax, 'b* (X)'),ylabel(ax,'a* (Y)'),zlabel(ax,'L* (Z)');
grid(ax,'on');
hold(ax,'on');
xlim(ax, [-128 128]);
ylim(ax, [-128 128]);
zlim(ax, [0 100]);
linewidth = 1;
line(ax,[-128 0],[0 0],[0 0],'color',[0 1 0],'lineWidth',linewidth); % Vert
line(ax, [0 128],[0 0],[0 0],'color',[1 0 0],'lineWidth',linewidth); % Rouge
line(ax, [0 0],[-128 0],[0 0],'color',[0 0 1],'lineWidth',linewidth); % Bleu
line(ax, [0 0],[128 0],[0 0],'color',[1 1 0],'lineWidth',linewidth); % Jaune
line(ax, [0 0],[0 0],[0 100],'color',[0 0 0],'lineWidth',linewidth); % Noir
[r,g,b] = meshgrid(linspace(0,1,20)); % linspace(0,1,50)
rgb = [r(:), g(:), b(:)];
LAB = rgb2lab(rgb, 'ColorSpace', 'srgb', 'WhitePoint','d50');
a = LAB(:,2);
b = LAB(:,3);
L = LAB(:,1);
k = boundary(a,b,L);
h1 = trisurf(k,a,b,L,'FaceColor','interp', 'FaceVertexCData',rgb,'EdgeColor','none', 'FaceAlpha', 0.30);
h1.Visible = 'on';
The line :
hold(ax,'on');
is supposed to allow me to overlay another plot and it works perfect in the context of a non-gridlayout. Somehow, the directive is ignored and I get the trisurf plot in a separate window... :(
I tried your code and ran into the weird following bug. If I leave the :
title(ax, '3D Scatter plot');
statement, the checkbox is not showing as you can see :
and I get an error message:
Unable to use a value of type matlab.graphics.axis.Axes as an index.
Error in PANTONE_subplots (line 36)
title(ax, '3D Scatter plot');
When I remove the statement entirely, leaving the figure without title, I get the correct result:
Now let's see if I can push your solution further. I'm glad to know now that uicontrol default units are pixel... Darn.
The error "Unable to use a value of type matlab.graphics.axis.Axes as an index." when executing the line
title(ax, '3D Scatter plot');
indicates that you have a variable called "title" interfering with the usage of the "title" function.
Do
clear title
to remove the variable from the base workspace.
Voss
Voss on 4 Mar 2024
Edited: Voss on 4 Mar 2024
BTW, descriptions and default values of uicontrol properties are listed here:
I went back to my "uipanel" solution and with the
hold (ax, 'on');
statement was honored :-)
So now, I'm continuing my work on that basis. Thank you, once again, for your help.
You're welcome! Any other questions, let me know. Otherwise, please "Accept" this answer. Thanks!

Sign in to comment.

Products

Release

R2023a

Asked:

on 4 Mar 2024

Commented:

on 5 Mar 2024

Community Treasure Hunt

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

Start Hunting!