Live script fprintf temporarily not printing whole line

조회 수: 6 (최근 30일)
Zel Hurewitz
Zel Hurewitz 2024년 8월 23일
편집: Jatin 2024년 9월 6일
While doing a bunch of file IO in a Live Script, and printing with fprintf, sometimes the line does not complete printing. Temporarily, it looks like the image below until the next iteration begins.
I was able to sort of recreate the effect with the code below, but what can be only a flicker in the execution of this snippet is much more noticable on my other project.
for i= 1:10
fprintf("Hello %d\n",i)
B= inv(rand(3000)+ 2*eye(3000));
end
Hello 1 Hello 2 Hello 3 Hello 4 Hello 5 Hello 6 Hello 7 Hello 8 Hello 9 Hello 10
Is this a bug or am I missing something?
  댓글 수: 2
Walter Roberson
Walter Roberson 2024년 8월 23일
This does not surprise me.
Output within Live Script is buffered. There are weird effects. For example
hold on
for i = 1 : 10
plot(rand(1,5));
fprintf("Hello\n");
end
plot(0.5,1,'*');
will display all of the text first and the plot at the end, but
hold on
for i = 1 : 10
plot(rand(1,5));
fprintf("Hello\n");
end
will display all except one of the text first, then the plot, then the last text.
dpb
dpb 2024년 8월 24일
Never use live script so no experience, but...I don't suppose a drawnow will make any difference here? Does disp act any differently?

댓글을 달려면 로그인하십시오.

답변 (1개)

Jatin
Jatin 2024년 9월 4일
편집: Jatin 2024년 9월 6일
I agree with what you mentioned, ‘fprintf’ does behave inconsistently in live editor. I’ve also faced the similar issue where the output of 'fprintf' is buffered.
For example,
for k = 1 : 10
fprintf('Hello: %d', k);
if k < 10
fprintf(",");
end
end
Hello: 1
,
Hello: 2
,
Hello: 3
,
Hello: 4
,
Hello: 5
,
Hello: 6
,
Hello: 7
,
Hello: 8
,
Hello: 9
,
Hello: 10
This code adds a new line with 'fprintf' in live editor which is not seen in standard script, as a workaround to the issue, Kindly check if wrapping the code inside a function as below helps your cause.
fun(10)
Hello: 1,Hello: 2,Hello: 3,Hello: 4,Hello: 5,Hello: 6,Hello: 7,Hello: 8,Hello: 9,Hello: 10
function fun(K)
for k = 1 : K
fprintf('Hello: %d', k);
if k < K
fprintf(",");
end
end
fprintf("\n");
end
Kindly refer to the following MATLAB Answer to know more on how to resolve this issue:
Hope this helps!

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by