How to resize axes programmatically within a GUI built with uifigure and uigridlayout?

5 views (last 30 days)
Hi everyone,
I've been building a GUI based on a uifigure and the matlab.apps.AppBase tools. It's not built in App Designer, I do it programmatically. I use a uigridlayout to position my various items; it works quite well for most things, but I'm having trouble with uiaxes.
These uiaxes are located within the uigridlayout, but I need to make a slight size adjustment after they've been created. I just need to adjust the width, I know by how many pixels to adjust it. If I run my app by typing "app = appName();" in the command window, the app shows up, the uiaxes are not adjusted. I then issue the command in the command window: "app.myAxes.Position(3) = app.myAxes.Position(3) - boxWidth;", which does exactly what I need it to do.
However, adding that line of code in the code that builds the actual GUI does not work, no matter where I try to put it. I've set AutoResizeChildren of the uifigure to off, no luck. I've searched here, but nothing I found seems to work.
So how do I get my app to execute that particular line of code?
Cheers,
Christiane
  8 Comments
dpb
dpb on 12 Sep 2024
Indeed, there are probably significant differences in your code and an appdesigner-created one...
classdef myGUI < matlab.apps.AppBase
properties
fGUI;
axesGTextes;
axesDTextes;
boxWidth;
backgroundcolor;
end
methods
function app = myGUI()
The appdesigner mini app I posted looks like
lassdef testResizeUIAxes_exported < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Button matlab.ui.control.Button
UIAxes matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
pos=app.UIAxes.Position;
pos(3)=0.75*pos(3);
app.UIAxes.Position=pos;
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
...
I'm guessing if you follow that template, then it will work as expected. In particular, the public properties and way they'r defined I suspect is where the problems lie -- and, a callback function is always expected to have the argument list of (app,event) which you're missing...
Christiane
Christiane on 12 Sep 2024
If I use a callback function from a button like you do in your template, it works, the resizing gets done. But I have to push the button.
My problem is that I want to do it when I build the GUI; the exact same lines of code, if called from the constructor rather than from a callback function, don't do anything. These same lines of code, if I put a breakpoint after creating the axes, (not before!) will work and be executed within the constructor.
Try and put a breakpoint in the code I posted, and see how the results differ if the breakpoint is after the axes creation. Tell me it's not a bug...

Sign in to comment.

Answers (1)

dpb
dpb on 12 Sep 2024
Edited: dpb on 13 Sep 2024
Here's a start; I needed a break from other issues of the day so I began to reorganize in line with the appdesigner layout, but it didn't seem to make any real difference, somewhat to my surprise.
However, since it worked to reduce the size of an UIAxes in a figure; I'm thinking it must have something to do with it being in a grid layout and somehow what that does/displays. --- Oh, one other thought...well, that didn't work, either. I thought maybe if one deleted the axes and then recreated it with the new size, but no; it used the new size but was still rendered the same as before. I'm pretty sure this must have something to do with how grids work but I've never used one so know absolutely nothing about their intricacies.
But, here's a file rearranged to be more in line with appdesigner structure; unfortunately, it didn't seem to make any difference in the symtpoms.

Categories

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!