Skip to content

Instantly share code, notes, and snippets.

@cwhy
Created August 6, 2015 13:14
Show Gist options
  • Save cwhy/b5d5561f62dffc270381 to your computer and use it in GitHub Desktop.
Save cwhy/b5d5561f62dffc270381 to your computer and use it in GitHub Desktop.
Multichannel Real Time Plot for Matlab
close all;
f = figure('Name', 'Visualization','Color', [1 1 1]);
set(f, 'menubar', 'none');
ax = axes();
hold(ax,'on')
set(ax, 'Unit', 'normalized');
set(ax, 'Position', [0.07 0.05 0.9 0.9])
% fakeSignal = repmat(sin(1:100),6,1);
N_Ch = 6;
colors = [[213,184,55];[233,89,78];[219 79 120];[87 28 93];[134 141 87];...
[13 56 62];[89 69 54];[185 160 126];[204 202 220];[128 157 167]];
colors = colors./255;
ChNames = {'Ch1', 'Ch2', 'Ch3', 'Ch4', 'Ch5', 'Ch6'};
tShow = 15; % Showing signal within the time window (in seconds)
fps = 20;
fShow = tShow*fps;
set(ax, 'YTick',50+(0:N_Ch-1)*100);
posCh = @(sig, ch) 80*sig+100*(ch-1)+50;
set(ax, 'YLim', [0 N_Ch*100]);
set(ax, 'YTickLabel', ChNames);
set(ax, 'XLim', [1 fShow]);
set(ax, 'XTick', 1);
f0 = 1;
f = 1;
fakeSignal = ones(1, N_Ch)*randn;
normSignal = zeros(1, N_Ch);
normS = @(s) (s -min(s))./(max(s)-min(s)) - 0.5;
bufSignal = zeros(fShow, N_Ch);
chPlts = zeros(1, N_Ch);
chDots = zeros(1, N_Ch);
ticks = zeros(5, 1);
tickLabels = char(zeros(5, 5));
set(gcf,'renderer','opengl');
for i = 1:N_Ch
chPlts(i) = line(1, posCh(0, i), ...
'Linesmoothing','on', 'color', colors(i,:), 'Parent', ax);
chDots(i) = plot(1, posCh(0, i), '.', 'MarkerSize',18, ...
'MarkerEdgeColor',[0.2 0.2 0.2], 'color', [0.2 0.2 0.2], ...
'Linesmoothing','on', 'Parent', ax);
end
t0 = clock;
MAX_Time = 60;
while etime(clock, t0) < MAX_Time
t = clock;
f0 = max(0, f-fShow);
set(ax, 'XLim', [1 fShow+fps] + f0);
fakeSignal = fakeSignal.*rand(1,N_Ch)+sin(f*(10+(1:N_Ch))*0.005.*(2+sin(f*(0.1+(1:N_Ch)*0.1)))+sin(f*(1:N_Ch)*0.1+randn(1,N_Ch)*0.2)*0.7+sin(1:N_Ch)*10);
signal = fakeSignal;
if f <= fShow
if mod(f, (fShow/5)) == 0
ticks(f/(fShow/5)) = f;
tickLabels(f/(fShow/5),:) = datestr(t, 'MM:SS');
set(ax, 'XTick', ticks(1:f/(fShow/5)));
set(ax, 'XTickLabel', tickLabels(1:f/(fShow/5),:));
end
bufSignal(f,:) = signal;
else
if mod(f, (fShow/5)) == 0
ticks = circshift(ticks, -1);
ticks(end) = f;
tickLabels = circshift(tickLabels, -1);
tickLabels(end,:) = datestr(t, 'MM:SS');
end
set(ax, 'XTick', ticks);
set(ax, 'XTickLabel', tickLabels);
bufSignal = circshift(bufSignal, -1);
bufSignal(end,:) = signal;
end
for i = 1:N_Ch
chData = posCh(normS(bufSignal(1:min(f, fShow),i)), i);
set(chPlts(i), 'XData',f0+1:f, ...
'YData', chData);
set(chDots(i), 'XData', f, ...
'YData', chData(end));
end
drawnow('expose')
f = f + 1;
pause(1/fps-clock+t)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment