Crowley
Crowley

Reputation: 2331

Order of graphics creation and data processing in MATLAB

I'm processing vast amount of data (>>100k lines) with variable step.

I would like to create figure and plot the processed data as they are counted (for termination of the running script when something goes wrong, or for impressing the "customers" with the fancy progress "animation").

Suppose we have `sourcefile.txt' with one column of positive numbers X(n) on row n and:

I've thought of:

fID=fopen('sourcefile.txt');
figure;axes; % create figure
act='0';threshold=0;N=0;DATA=0;ii=0;
while ischar(act) % loop until end of file.
  ii=ii+1;N=0;
  while temp<threshold&&ischar(act) % loop until threshold is overflown (next step) or end-of-file is reached. 
    act=fgetl(fID);
    temp=temp+str2double(act);
    N=N+1;
  end
  line('xdata',ii,'ydata',temp/N,'Marker','o') % New point should appear
threshold=threshold+0.5; % Continue with new stopping rule.
end

The problem is that the loops are entered and processed and then figure is created. How can I force MATLAB to create a figure and then to calculate/update plotted data?

Upvotes: 1

Views: 54

Answers (1)

rayryeng
rayryeng

Reputation: 104545

Try inserting drawnow after you draw the line using line. This will flush the graphics buffer and update your figure immediately. If you don't do this, your figure will appear blank until the loop finishes, and then the figure will appear with your objects that you added to the figure.

So to be specific with what I talked about:

fID=fopen('sourcefile.txt');
figure;axes; % create figure
act='0';threshold=0;N=0;DATA=0;ii=0;
while ischar(act) % loop until end of file.
  ii=ii+1;N=0;
  while temp<threshold&&ischar(act) % loop until threshold is overflown (next step) or end-of-file is reached. 
    act=fgetl(fID);
    temp=temp+str2double(act);
    N=N+1;
  end
  line('xdata',ii,'ydata',temp/N,'Marker','o') % New point should appear

  %%%%%%%%%%%%// Change here
   drawnow;

threshold=threshold+0.5; % Continue with new stopping rule.
end

Therefore, once you draw a line, the figure should immediately update itself and so for each iteration, the figure should update with a new line object.

Upvotes: 2

Related Questions