Call uitreenode as cellfun in App Designer method

4 views (last 30 days)
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 Comment
Dolon Mandal
Dolon Mandal on 20 Sep 2023
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!

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!