Aligning controls in a Matlab script

11 次查看(过去 30 天)
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 个评论
Angelo Yeo
Angelo Yeo 2023-12-18
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.
Rik
Rik 2023-12-18
Can you confirm you are working in a live script and not building a GUI?

请先登录,再进行评论。

回答(1 个)

Pratyush
Pratyush 2023-12-18
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

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by