Generate edit text boxes by providing their numbers in GUI

2 ビュー (過去 30 日間)
Ahmed Anas
Ahmed Anas 2019 年 8 月 2 日
編集済み: Kanishk 2024 年 12 月 23 日
I want to generate n number of edit text boxes by providing n as input in GUI. Is it possible?
For example,
User first provide value of n. Let it be 5
Then 5 edit boxes will be generated and then user provide values in them.
  1 件のコメント
Rik
Rik 2020 年 1 月 31 日
This is fairly easy with a programmatic GUI, as you can create the uicontrol objects in a loop and determine the Position based on the number of inputs.

サインインしてコメントする。

回答 (1 件)

Kanishk
Kanishk 2024 年 12 月 23 日
編集済み: Kanishk 2024 年 12 月 23 日
Following Rik's comment, here is a simple function using loops to generate edit field.
function generateDynamicEditFields()
fig = uifigure('Name', 'Dynamic Edit Fields');
numField = uieditfield(fig, 'numeric', 'Position', [180 250 100 22]);
generateButton = uibutton(fig, 'push', 'Text', 'Generate', ...
'Position', [300 250 70 22], ...
'ButtonPushedFcn', @(btn, event) generateEditFields(fig, numField.Value));
end
function generateEditFields(parentFig, n)
delete(findall(parentFig, 'Type', 'uieditfield'));
for i = 1:n
uieditfield(parentFig, 'text', ...
'Position', [20, 250 - i*30, 350, 22]);
end
end
To add, you can use 'delete' and 'findall' to remove previously generated Edit fields.
delete(findall(parentFig, 'Type', 'uieditfield'));
Executing the 'generateDynamicEditFields' generates the follwing figure.
generateDynamicEditFields
You can learn more about 'uieditfield', 'delete' and 'findall' from the following commands.
web(fullfile(docroot, 'matlab/ref/uieditfield.html'))
web(fullfile(docroot, 'matlab/ref/delete.html'))
web(fullfile(docroot, 'matlab/ref/findall.html'))
Hope that helps!

カテゴリ

Help Center および File ExchangeSpecifying Target for Graphics Output についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by