Unable to display uicontrol within a given panel?

4 visualizzazioni (ultimi 30 giorni)
Roger Breton
Roger Breton il 4 Mar 2024
Commentato: Voss il 5 Mar 2024
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 Commento
Roger Breton
Roger Breton il 4 Mar 2024
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...

Accedi per commentare.

Risposte (1)

Voss
Voss il 4 Mar 2024
Modificato: Voss il 4 Mar 2024
"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 Commenti
Roger Breton
Roger Breton il 5 Mar 2024
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 il 5 Mar 2024
You're welcome! Any other questions, let me know. Otherwise, please "Accept" this answer. Thanks!

Accedi per commentare.

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by