Call uitreenode as cellfun in App Designer method

3 次查看(过去 30 天)
Hello, i need to speed up my code which should generate a uitreenode with many entries (more than 1500). My current implementation uses a for loop and it takes to much time. The idea was to speed it up using cellfun because all entries are listet in a cell. But i was not able to set it up the right way.
Current implementation:
for i = 1:length(channelGroup)
parent = uitreenode(app.Tree, "Text", channelGroup(i).AcquisitionName);
tt = data{i};
nVarNames = length(tt.Properties.VariableNames);
nChannelGroups = length(channelGroup);
child = zeros(1,nVarNames); %preallocate, better performance
for j = 1:length(tt.Properties.VariableNames)
%wb = waitbar((j/nVarNames),wb,['Set Up ChannelGroup ' num2str(i) '/' num2str(length(channelGroup))]);
child(j) = uitreenode(parent, "Text", tt.Properties.VariableNames{j});
end
end
Using
cellfun (@uitreenode, parent, "Text", tt.Properties.VariableNames)
does not make the job.
Can you help me set it up the right way?
  1 个评论
Dolon Mandal
Dolon Mandal 2023-9-20
To speed up your code, you can try using the `arrayfun` function instead of `cellfun`. Here's an updated implementation that should help improve the performance:
for i = 1:length(channelGroup)
parent = uitreenode(app.Tree, "Text", channelGroup(i).AcquisitionName);
tt = data{i};
nVarNames = length(tt.Properties.VariableNames);
nChannelGroups = length(channelGroup);
child = arrayfun(@(j) uitreenode(parent, "Text", tt.Properties.VariableNames{j}), 1:nVarNames);
end
In this updated code, `arrayfun` is used to create an array of `uitreenode` objects directly, avoiding the need for an inner loop. The anonymous function `@(j)` is used to iterate over the indices `1:nVarNames` and create the `uitreenode` objects with the appropriate text.
Note that the `wb` waitbar is commented out in this code snippet. If you need to include it, you can uncomment it and update the progress calculation accordingly.
I hope it helps!

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by