Create variable Edit Fields based on a calculated variable in App Designer

12 次查看(过去 30 天)
The app asks for the Number of Outlets . When the Number of Outlets is specified, for example let's say 256 outlets, the app calculates the number of Splits by using the following equation:
Split_Number=log2(Outlet_Number)
Now, the software asks for inputs from the user. The number of requested inputs is equal to the Split Number. For example, for 265 outlets, Split number will be 8. So, I want the app to show 8 Edit Field (numeric) boxes for entering 8 numeric inputs (D1...D8)..Or for example if I have 64 outlets, the app asks for 6 inputs (D1--D6).
I could not figure out how to do it in MATLAB App Designer. Could you please help me with this? I could generate an input dialog box with varaible number of boxes. However, I could not do it in the app designer to generate variable input boxes.

回答(1 个)

Tridib
Tridib 2025-6-11,11:44
Unlike " inputdlg", which works in a pop-up, to dynamically generate a variable number of numeric input fields in MATLAB App Designer, you need to create UI components programmatically inside a container like a " Panel".
Here is a workaround that works well:
1. Add a "Edit Field(Numeric)", "Button" and a "Panel".
2. Add this code to "EnterInputsButtonPushed" callback:
% clears any previous content
delete(app.InputsPanel.Children);
Outlet_Number = app.NumberofOutletsEditField.Value;
Split_Number = log2(Outlet_Number);
if mod(Split_Number, 1) ~= 0
uialert(app.UIFigure, ...
'Number of outlets must be a power of 2 (e.g., 2, 4, 8, ...)', ...
'Invalid Input');
return;
end
% creates a grid layout inside the panel to enable
% scrolling in the grid layout. This is helpful when
% Split_Number is large and not all input fields fit
% in the panel
grid = uigridlayout(app.InputsPanel, [Split_Number, 2]);
grid.Scrollable = true;
%'fit' makes each row just large enough to fit its contents
% and this is repeated for each row
grid.RowHeight = repmat({'fit'}, 1, Split_Number);
% adding labels and numeric fields to the grid
for i = 1:Split_Number
uilabel(grid, ...
'Text', sprintf('D%d:', i));
uieditfield(grid, ...
'numeric');
end
For more information, refer to the following documentations:
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by