MATLAB: Continue displaying output using disp() function

displaydisplay outputlive scriptMATLABParallel Computing Toolbox

I use disp() in a live script function to track which samples in a data set have been processed in a parfor loop. However, when long enough, the disp() out put will be something like this:
Sample 300 has been processed.
Sample 301 has been…
and will no longer output.
I've looked around and can't seem to how to remove an output limitation. Is there a way to increase/unlimit this or is it immutable?

Best Answer

In this case, your code is hitting a 60,000 character limit per output. This particular limit is part of the Live Editor, and unfortunately, there is no way to change it. Our development team will take this as feedback, and we hope to address it in a future release.
That being said, there might be some ways you can work around this limit. Although I’m not sure they’ll meet your needs, as the first two don’t work with parfor.
Workaround 1:
The Live Editor automatically groups textual output from the same line into a single output. That means that if we introduce some other output on a different line, we can have multiple outputs, each with their own 60,000 character limit. Here is a crude example that demonstrates this. The output doesn’t look so nice, but you can see all the progress.
for ind=1:10000
if (mod(ind, 1000) == 0)
disp('--')
end
disp(['Sample ', int2str(ind), ' has been processed'])
end
Result of Workaround 1
Workaround 2:
Some people have used the ‘\b’ (backspace) character to create textual progress indicators (e.g. https://blogs.mathworks.com/loren/2007/08/01/monitoring-progress-of-a-calculation/). I believe there are some tools for this on File Exchange, but as an example, you could do something like the following. Once again, it is a crude example only meant to demonstrate the concept.
for ind=1:20
pause(.1)
printStatus(ind) % Do the fprints in a local function so that all the textual output is coming from one line in the script.
end
function printStatus (ind)
if ind == 1
% The very first time we don't need to delete the old text
fprintf('Last processed sample: %5d', ind);
else
% Each \b removes one character from the previous fprintf.
fprintf('\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bLast processed sample: %5d', ind);
end
end
Results of Workaround 2
Workaround 3:
Since the textual output for the entire parfor is treated as one output and hence subject to the 60,000 character limit, you could potentially split up your work into a few parfors on separate lines:
parfor ind=1:1000
doStuff(ind);
end
parfor ind=1001:2000
doStuff(ind);
end
function doStuff(ind)
disp(['Sample ', int2str(ind), ' has been processed'])
end
Results of Workaround 3
Not a great solution, but sharing just in case.
I hope this helps, and as I said earlier, we hope to address this in a future release.