Loop through grid layout takes FOREVER. How can I optimize the code?
11 次查看(过去 30 天)
显示 更早的评论
I am trying to create an app with many text areas (80x12) in a gridlayout. I want to fill each grid with a individual text taken from a cell array. However, in the code example below I am just using {['test1',newline,'test2']} for all of them. But in the real example each text.value is different. I am looping over each textarea in the gridlayout and it is working as intended. However, it takes FOREVER (75.101076 seconds). Is there a way to speed up the process?
tic
fig = uifigure('Position',[100 100 440 320]);
%fig.WindowState = 'maximized';
g = uigridlayout(fig,[80 12],'Scrollable','on');
g.ColumnSpacing = 0;
g.RowSpacing = 0;
g.ColumnWidth = {150,50,20,50,150,50,50,50,50,50,150,'fit'};
g.RowHeight = {50,50,50,50,50,50,50,50,50,50,...
50,50,50,50,50,50,50,50,50,50,...
50,50,50,50,50,50,50,50,50,50,...
50,50,50,50,50,50,50,50,50,50,...
50,50,50,50,50,50,50,50,50,50,...
50,50,50,50,50,50,50,50,50,50,...
50,50,50,50,50,50,50,50,50,50,...
50,50,50,50,50,50,50,50,50,50};
for j = 1:12
for i = 1:80
txa = uitextarea(g,'Value', {['test1',newline,'test2']});
txa.Layout.Row = i;
txa.Layout.Column = j;
if i == 3
txa.BackgroundColor = 'yellow';
end
if i == 8 && j == 5
txa.BackgroundColor = 'green';
end
end
end
toc
Elapsed time is 75.101076 seconds.
Below is an example of the output
0 个评论
回答(1 个)
Deepak
2024-11-6,7:45
To optimize the performance of the MATLAB code, creating many text areas in a grid layout, we can implement several optimizations aimed to reduce the computational overhead.
First, we can pre-allocate an array to store the handles of the text areas, which can help improve performance by minimizing dynamic memory allocation during the loop iterations. Next, we can separate the text areas creation logic from properties customization logic, to reduce the complexity during the initial creation phase. Finally, we can use “repmat” function for setting uniform row heights to slightly enhance the performance by reducing complexity of layout assignments.
Here is the updated MATLAB code with the required changes:
tic
fig = uifigure('Position', [100 100 440 320]);
g = uigridlayout(fig, [80 12], 'Scrollable', 'on');
g.ColumnSpacing = 0;
g.RowSpacing = 0;
g.ColumnWidth = {150, 50, 20, 50, 150, 50, 50, 50, 50, 50, 150, 'fit'};
g.RowHeight = repmat({50}, 1, 80); % Use repmat for cleaner code
% Preallocate text areas
textAreas = gobjects(80, 12);
% Create text areas
for j = 1:12
for i = 1:80
textAreas(i, j) = uitextarea(g, 'Value', {['test1', newline, 'test2']});
textAreas(i, j).Layout.Row = i;
textAreas(i, j).Layout.Column = j;
end
end
% Set background colors in a separate loop
for j = 1:12
for i = 1:80
if i == 3
textAreas(i, j).BackgroundColor = 'yellow';
end
if i == 8 && j == 5
textAreas(i, j).BackgroundColor = 'green';
end
end
end
toc
Elapsed time is 3.847896 seconds.
Please find attached the documentation of functions used for reference:
I hope this helps in resolving the issue.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!