Unable to display uicontrol within a given panel?
Show older comments
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');
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
Roger Breton
on 4 Mar 2024
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
Roger Breton
on 4 Mar 2024
Roger Breton
on 4 Mar 2024
Voss
on 4 Mar 2024
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.
BTW, descriptions and default values of uicontrol properties are listed here:
Roger Breton
on 5 Mar 2024
Voss
on 5 Mar 2024
You're welcome! Any other questions, let me know. Otherwise, please "Accept" this answer. Thanks!
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!



