1

I have a few files which I am planning to plot in Matlab. I have loaded the files and I wanted to plot it in real time. As in I want to see it as it is being plotted.

%clear
[filename, pathname] = uigetfile('*.raw;*.prc', 'Pick raw or processed data file');
N=str2double(filename(5:6));
Fs = 145.3*10^3; 
T = 1/Fs; % Sampling period
if isequal(filename(end-2:end),'raw')
    % load raw data file
    fid = fopen([pathname filename],'r','l');
    A=fread(fid,inf,'*uint16')'; %s=ftell(fid); 
    B=bitand(A, hex2dec('0FFF')); C=bitshift(A,-12);
    channels=sort(C(1:N));
    rawdata(int16(numel(A)/N)+2,N)=uint16(0); % Need to add +2 due to data loss
    for ii=1:N
        %rawdata(:,ii)=B(C==channels(ii)); can't use due to data loss
        temp=B(C==channels(ii));
        rawdata(1:numel(temp),ii)=temp;
    end
    plot(3.3/4095*single(rawdata))
    legend(int2str(channels'))
else
    %load processed file
    fid = fopen([pathname filename],'r','b');
    A= fread(fid,inf,'*single')';
    prcdata=reshape(A,N,[])';


    %Find time
    N = size(prcdata(:,1),1); 
    t=T*(0:N-1)'; 

for u = 1:N;
        x=0:(T*(0:u-1)');
        plot(x,prcdata(:,4));
   drawnow;
   end
end
Matlaber
  • 35
  • 7
  • If your plots aren't showing up as you process, you might need to add a `pause(0.001)` after your plotting function, to allow time for your computer to render the graphic. – qbzenker Jul 05 '17 at 02:53
  • tried adding a pause(0.001) it is taking time to be processed. Also was wondering if it will work if I just add the pause without the for loop like the code below `code %Find time N = size(prcdata(:,1),1); t=T*(0:N-1)'; x=0:(T*(0:u-1)'); plot(x,prcdata(:,4)); drawnow; end` – Matlaber Jul 05 '17 at 04:24
  • I used the solution sometime back. probably it will help [maybe this post can help you](https://stackoverflow.com/questions/8278368/matlab-real-time-plot) – Belgarath Jul 05 '17 at 09:33
  • thank you Belgarath I will just have a look at that post I have used that code and have kept it to run will take a while as these data is large. – Matlaber Jul 05 '17 at 23:38

2 Answers2

0

try animinatedline

objects optimize line animations by accumulating data from a streaming data source. After you create the initial animated line using the animatedline function, you can add new points to the line without having to redefine the existing points. Modify the appearance of the animated line by setting its properties.

Belgarath
  • 104
  • 12
  • does that require the graph to be plotted first and replot it with animated line function? – Matlaber Jul 05 '17 at 23:40
  • i tried with the animated line function with the following code `code %Find time N = size(prcdata(:,1),1); t=T*(0:N-1)'; figure; hold on; h = animatedline; %axis([0,4*pi,-1,1]) x = t; y = prcdata(:,4); for k = 1:N addpoints(h,x(k),y(k)); drawnow end end` its giving me an error saying that the its invalid type for argument Y. Type should be double – Matlaber Jul 06 '17 at 01:54
  • try y = prcdata(:,6) – Belgarath Jul 06 '17 at 06:18
  • It still has the same error. when i tried with y=prcdata(:,6) i don't think changing the channels work as they have a 6 channel data with millions of data points depending on the sampling frequency – Matlaber Jul 06 '17 at 06:41
0

I know one heuristic way but it certainly works:

for u = 1:N;
    x(u)=0:(T*(0:u-1)');
    plot(x(1:u),prcdata(:,4));
    pause(0.01);
end
Yacine
  • 321
  • 2
  • 15