# Why plotting colocrbar slows down the performance of MATLAB

4 views (last 30 days)
Ahmad Gad on 22 Dec 2020
Commented: Ahmad Gad on 4 Jan 2021
Hello everyone. I have a question that I am not able to solve.
I have a cell array 'Temp' that defined the frames of a thermal video. Each cell defines an intensity image of the video. When I plot them using this:
tic
for i = 1:size(Temp,1)
imagesc(Temp{i});
drawnow;
end
toc
Elapsed time is 16.539161 seconds.
When I try to include the colorbar to get some unmerical idea about the thermal images, using the following code:
tic
for i = 1:size(Temp,1)
imagesc(Temp{i});
colorbar; % I just asked for the colorbar to be added
drawnow;
end
toc
Elapsed time is 79.288858 seconds.
Why this big change in the performance of the code when I add the colorbar? Can anyone explain or tell a workaround?
Thanks all and best,
Ahmad
##### 2 CommentsShow 1 older commentHide 1 older comment
Ahmad Gad on 22 Dec 2020
Thanks Subhadeep for the reply.
calling it before the loop doesn't work, due to the reason you mentioned. Do you suggest another function other than imagesc(Temp{i})?
I may have to change this to get things to work fine.
Thanks!

Sign in to comment.

### Accepted Answer

Walter Roberson on 22 Dec 2020
for K = 1 : 60; Temp{K} = rand(320,240); end
tic
fig = figure();
ax = axes(fig);
for i = 1:size(Temp,1)
imagesc(ax, Temp{i});
drawnow;
end
toc
Elapsed time is 0.266523 seconds.
tic
fig = figure();
ax = axes(fig);
for i = 1:size(Temp,1)
imagesc(ax, Temp{i});
colorbar(ax); % I just asked for the colorbar to be added
drawnow;
end
toc
Elapsed time is 0.581383 seconds.
Yup, colorbar added was a lot slower.
tic
fig = figure();
ax = axes(fig);
colorbar(ax); % I just asked for the colorbar to be added
hold(ax, 'on');
h = imagesc(ax, Temp{1});
for i = 2:size(Temp,1)
h.CData = Temp{i};
drawnow;
end
hold(ax, 'off')
toc
Elapsed time is 0.141660 seconds.
But being careful about using handles is faster.
##### 1 CommentShow NoneHide None
Ahmad Gad on 4 Jan 2021
Walter, thanks a lot for the reply.
Can you modify this line,
for i = 2:size(Temp,1)
to be,
for i = 2:size(Temp,2)
So, the code can work easily for new readers?
Thanks!

Sign in to comment.

### Categories

Find more on Desktop in Help Center and File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!