How to speed up dynamic allocation, deletion of uicontrols?

11 views (last 30 days)
Florian B
Florian B on 5 Nov 2021
Edited: Florian B on 5 Nov 2021
One part of my uicontrol application display a series of parameters from an INI-file that can be modified. For each I want to display generate a row consisting of one uilabel, uitextbox, uibutton each. This is what it looks like:
I also want to display only a set of parameters based on the section of the INI config file (e.g. [Paths], [Limits]). The section can be changed via a dropdown combobox (not shown here). This means the number of rows/parameters changes during runtime and I need to create or destroy rows depending on the number of paramters, also during runtime (i always destroy all rows before creating the new amount of new rows):
arrData = ini.GetParameters(); % retrieves keys and values from INI config file
obj.deleteParamRows(); % delete ALL old rows (btn, edt, lbl)
obj.createParamRows(arrData); % create new rows for current set of parameters passed with arrData
I choose to create the controls dynamically and in the form of an array. This way I can loop through the controls and initialize each row, e.g. set parameter label titles and values. Here is the minimum code example for the generation of the labels (same with button and edit):
arrLbls = matlab.ui.controls.uilabel.Empty(num_array); % preallocate array of labels (empty)
for i=1:len(arrLbls) % create the uilabel for each row
arrLbls(i) = uilabel(parent, "Specific Label Title");
arrLbls(i).Layout ... % add to uigridlayout of panel
% .. further initialization, e.g. callback function assignment
end
The code works fine but is slow. Execution takes up to 30 seconds, although only when up to 15 - 20 parameters (=sum of 3 x 20 controls) are generated. While this may make sense due to the high number of controls - spread over three arrays - the deletion of the controls is also pretty slow, altough the code is pretty simple:
delete(arrLbls(:)); % delete each label (uicontrol)
arrLbls = []; % delete array of label (pointers?)
The two lines above require up to 10 - 20 seconds.
Is there a way to increase the speed of my code? Is there a function like C realloc() which lets me expand/shink the array?
(Please excuse any errors you may spot but I only have access to Matlab at work and typed the code off my memory.)

Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!