필터 지우기
필터 지우기

Unable to display uicontrol within a given panel?

조회 수: 8 (최근 30일)
Roger Breton
Roger Breton 2024년 3월 4일
댓글: Voss 2024년 3월 5일
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
Roger Breton
Roger Breton 2024년 3월 4일
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...

댓글을 달려면 로그인하십시오.

답변 (1개)

Voss
Voss 2024년 3월 4일
편집: Voss 2024년 3월 4일
"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
Roger Breton 2024년 3월 5일
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.
Voss
Voss 2024년 3월 5일
You're welcome! Any other questions, let me know. Otherwise, please "Accept" this answer. Thanks!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by