MATLAB Answers

Controlling current axes within programmatic UI (GUI layout toolbox)

7 views (last 30 days)
D. Plotnick
D. Plotnick on 30 Mar 2016
Commented: John BG on 19 Oct 2017
I am currently building a programmatic UI using the GUI layout toolbox (GLT), and I am having difficulty convincing Matlab to use the axes that I want for plotting purposes. I am including a MWE below (note, it requires the GLT, although I do not believe that is the source of the issue).
The issue is that as written below the scatter3 plots in a new figure window that it opens itself, not the UI axes.
function gui = guiTest
gui = struct();
% Initialize Window
scrsz = get(groot,'ScreenSize');
gui.Window = figure( ...
'Name', 'A Test Window', ...
'NumberTitle', 'off', ...
'MenuBar', 'none', ...
'Toolbar', 'none', ...
'HandleVisibility', 'off',...
'OuterPosition', [3*scrsz(4)/4 scrsz(4)/2 scrsz(3)/2 scrsz(4)/2]);
% Create Main Layout
mainLayout = uiextras.HBoxFlex(...
% Left Side Control Panel
controlPanel = uiextras.BoxPanel(...
'Title','Controls go here');
% Right Side Plotting
gui.ViewPanel = uiextras.BoxPanel(...
'Parent', mainLayout,...
'Title','Axes Panel Name');
% Relative sizes of main layout sections
set(mainLayout, 'Sizes', [-1 -2]);
%Right Side Plotting %
gui.ViewAxes = axes(...
X = rand(100,1); Y = rand(100,1); Z = rand(100,1);
scatter3(X,Y,Z); axis equal;
Note that I have solved the issue my changing the last line of the above to:
scatter3(X,Y,Z,'Parent',gui.ViewAxes); axis(gui.ViewAxes, 'equal');
Which may just be how I have to work from now on. However, my question is why the command to switch axes using axes(gui.ViewAxes) does not actually seem to make the active figure/axes pair remain as the current axes. This is why gca is a dangerous command to use, but I would like to understand if there is some UI handle shenanigans that I am simply not understanding.
Please, no suggestions that I go back to GUIDE.
Thanks in advance!


Sign in to comment.

Accepted Answer

Robert on 30 Mar 2016
gca calls gcf and checks the 'CurrentAxis' property of the result. Because you turned you figure's 'HandleVisibility' to 'off', gcf cannot find it and creates a new figure instead.


D. Plotnick
D. Plotnick on 30 Mar 2016
facepalm. Yes...yes I did. Thanks!
I also just posted using a similar MWE regarding the rotate3d tool not refreshing the plot when used. Switching the 'HandleVisibility' to 'on' does not seem to solve that problem.

Sign in to comment.

More Answers (0)

Translated by