Generate edit text boxes by providing their numbers in GUI

2 views (last 30 days)
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 Comment
Rik
Rik on 31 Jan 2020
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.

Sign in to comment.

Answers (1)

Kanishk
Kanishk on 23 Dec 2024
Edited: Kanishk on 23 Dec 2024
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!

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!