I should add plotting appears to be proportional to the plot size and I like to have larger plots :)

# scrolling plot speed and axis jitter

16 views (last 30 days)

Show older comments

I'd like to create auto-scrolling plots in which data scrolls by, automatically, as it gets collected (from a DAQ). Before worrying about adding data I wanted to first check how plots behave with a pre-defined data vector where I adjust the x-limits to simulate scrolling. Just to clarify, I don't intend to have any manual scrolling, just calls to adjust the axis limits.

This is the baseline code for plotting:

t = 0:1/1000:20;

s = rand(1,length(t)).*sin(0.67*2*pi*t);

ax(1) = subplot(3,1,1);

h1 = plot(t,s);

toc

ax(2) = subplot(3,1,2);

h2 = plot(t,s);

ax(3) = subplot(3,1,3);

h3 = plot(t,s);

Now I'd like to do something like the following:

update_tick = 0;

set(ax,'ylim',[-1 1])

tic

for t2 = 0:0.01:18

set(ax,'xlim',[t2 t2+2]) %Move the data view

update_tick = update_tick + 1;

%At some interval rerender the ticks

if mod(update_tick,5) == 0

xtick = t2:0.1:t2+2;

set(ax,'XTick',xtick)

end

drawnow

%pause(0.005)

end

toc

The code above changes to the x-limits to simulate scrolling/new data and occasionally updates the displayed ticks.

This code however is horribly slow. This takes about 50 seconds to run on my computer. It gets faster if the drawnow is replaced with pause (about 12s) but I have other things to do in the DAQ collection loop and I'd like this number to be closer to 1-2 seconds. For reference, Matlab 2017b documentation states that using the pause function is equivalent to the drawnow command, but I find I like this explanation better (basically a drawnow if possible within allotted time): https://www.mathworks.com/matlabcentral/answers/7309-is-pause-a-superset-of-drawnow#answer_192103

Additionally, as the plotting is occurring the axes are shaking just slightly which is really annoying.

Any suggestions on speeding up the code?

##### 3 Comments

### Answers (1)

Joshua Robinson
on 29 Apr 2019

Your main problem is that you're forcing matlab to recalculate and update the position and names of all the ticks every couple of iterations. So even if t2 changed by a little bit, every single tick is recalculated.

Instead it would make more sense to only update the tick values that actually change as t2 moves along (aka the first and last ticks).

The quickest way to achieve this is to round t2 to the nearest tick value and add a tick offset array to the rounded t2 value ala...

dtick = 0.1;

tickbase = (0:dtick:2);

% main loop

for t2=tmin:dt:tmax;

% BLAH BLAH BLAH

% calculate new ticks

nearest_tick = dtick*round(t2/dtick);

xtick = nearest_tick + tick_offset;

set(ax,'XTick',xtick)

% BLAH BLAH BLAH

end

So the final code should be something along the lines of...

clear all

close all

t = 0:1/1000:20;

s = rand(1,length(t)).*sin(0.67*2*pi*t);

ax(1) = subplot(3,1,1);

h1 = plot(t,s);

toc

ax(2) = subplot(3,1,2);

h2 = plot(t,s);

ax(3) = subplot(3,1,3);

h3 = plot(t,s);

% my edit

ax(1).XGrid = 'on';

ax(1).YGrid = 'on';

ax(2).XGrid = 'on';

ax(2).YGrid = 'on';

ax(3).XGrid = 'on';

ax(3).YGrid = 'on';

dtick = 0.1;

tick_offset = (0:dtick:2);

update_tick = 0;

set(ax,'ylim',[-1 1])

tic

for t2 = 0:dtick:18

set(ax,'xlim',[t2 t2+2]) %Move the data view

update_tick = update_tick + 1;

% %At some interval rerender the ticks

if mod(update_tick,5) == 0

nearest_tick = dtick*round(t2/dtick); % Code from above

xtick = nearest_tick + tick_offset;

set(ax,'XTick',xtick)

end

drawnow;

end

toc

##### 3 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!