Aligning controls in a Matlab script
8 views (last 30 days)
Show older comments
I have edit fields in a Matlab script for user input of numerical parameters. How do I align the input field boxes, for example, so that they have the same end position on the right, when code is hidden? In the example below, I want the boxed fields to be aligned right, rather than having the text aligned left, which causes the boxes to be staggered. This is a simple Matlab script, with inserted edit fields. The text is the "label" in the configuration of the control. Many thanks in advance!
2 Comments
Angelo Yeo
on 18 Dec 2023
In my R2023b (Update 4), the edit fields are automatically aligned when codes are hidden. Can you specify the reproduction steps for your issue? Or you can attach the live script with the issue.
Answers (1)
Pratyush
on 18 Dec 2023
Hi Peter,
I understand that you want to align the edit fields so that they have the same end position on the right.
The 'Position' property is a four-element vector [left bottom width height] that defines the size and position of the control. You can refer to the following link for more details: https://in.mathworks.com/help/matlab/ref/matlab.ui.figureappd-properties.html#d126e1937327
You need to ensure that the sum of the 'left' and 'width' properties is the same for all edit fields.
Here's an example of how you might do this programmatically:
% Define the width and right edge position of the edit fields
editFieldWidth = 100; % Width of the edit fields
rightEdgePosition = 300; % Position of the right edge of the edit fields
% Calculate the 'left' position for each edit field so that the right ends align
editField1Left = rightEdgePosition - editFieldWidth;
editField2Left = rightEdgePosition - editFieldWidth;
% ... do this for each edit field
% Create the edit fields with aligned right ends
editField1 = uicontrol('Style', 'edit', 'Position', [editField1Left 200 editFieldWidth 20]);
editField2 = uicontrol('Style', 'edit', 'Position', [editField2Left 170 editFieldWidth 20]);
% ... create more edit fields as needed
0 Comments
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!