How to resize axes programmatically within a GUI built with uifigure and uigridlayout?
Afficher commentaires plus anciens
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 commentaires
dpb
le 12 Sep 2024
Attach a minimum working example so folks can play around with it; unfortunately, the forum doesn't support the uifigure so it can't be checked out here.
drawnow is sometimes required to force updating the HG2 figures; I dont know if it is effective with the uifigure or not, but you could try adding one after the resizing code line to see...
Alternatively, try
pos=app.myAxes.Position;
pos(3)=pos(3)-boxWidth;
app.myAxes.Position=pos;
Christiane
le 12 Sep 2024
Christiane
le 12 Sep 2024
dpb
le 12 Sep 2024
I have only done the few apps I've done through appdesigner and they have barebones minimal components so I'm not a good one for more complex things. But, your comment
" But the program just doesn't seem to execute those lines, or else there's another function that resizes the GUI after I've issued them."
is sorta' what I'm thinking must be going on behind the scenes; have you tried setting a breakpoint in the app after the resize call to see if, indeed, it is getting executed and the change happens but then gets blown away later by something behind the scenes? If it functions as expected from command line and not in the application code, then it has to be something between the two; if it were first done and then lost, that would tell you something, at least, even if not yet a solution.
Secondly, I wonder about the grid layout container possibly being the culprit; have you tried programmatically building a test application that does the single component and resizes it and get it to work, then keep adding pieces around it until you break it?
Failing that any of those options help, it's probably time to submit the MWE to official support if somebody doesn't come along "real soon now"...
Christiane
le 12 Sep 2024
I don't know what is different in your class for the app built programmatically, but I just build an app w/ appbuilder consisting of an axes and a button; the button call back was to reduce the axes width by 25% each time it was pressed.
That functioned as expected, so there's something going on in the way appbuilder builds one and your version. I did it under R2021b, however, so I suppose it's possible some bug got introduced since.
See if the below has the same/similar issue on your system...
dpb
le 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
le 12 Sep 2024
Réponses (1)
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.
Catégories
En savoir plus sur Develop Apps Using App Designer dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!