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');
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개)
"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
Roger Breton
2024년 3월 4일
Roger Breton
2024년 3월 4일
Voss
2024년 3월 4일
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
2024년 3월 5일
Voss
2024년 3월 5일
You're welcome! Any other questions, let me know. Otherwise, please "Accept" this answer. Thanks!
카테고리
도움말 센터 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




