Inconsistent behaviour in fprintf output (scripts and LiveScripts)?

6 ビュー (過去 30 日間)
John
John 2021 年 3 月 12 日
コメント済み: Adam Danz 2021 年 3 月 24 日
The output from fprintf seems to be inconsistent when called from within a livescript. The interpreter seems to be adding extra newlines after each call.
For example take the following code:
K = 10;
for k=1:K
fprintf('Count: %d',k);
if k < K
fprintf(', '); %Add a comma for all but the last number
end
end
fprintf('\n')
If you execute this code in a standard script, the output is formatted correctly:
Count: 1, Count: 2, Count: 3, Count: 4, Count: 5, Count: 6, Count: 7, Count: 8, Count: 9, Count: 10
If executed in a LiveScript, the output looks much different:
Count: 1
,
Count: 2
,
Count: 3
,
Count: 4
,
etc.
Is there a way to supress these extra newlines? Is this intended behaviour? Is there an elegant way of getting around this? (I could assemble the output string, before calling fprintf, but that seems cumbersome)
  2 件のコメント
John
John 2021 年 3 月 24 日
Yup. Just did that. Thanks.

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2021 年 3 月 24 日
編集済み: Adam Danz 2021 年 3 月 24 日
I agree that the behavior in LiveEditor is inconsistent.
This version is better though still displays the final text on a separate line in LiveEditor.
K = 10;
for k=1:K
if k < K
fprintf('Count: %d, ',k)
else
fprintf('Count: %d\n',k)
end
end
Count: 1, Count: 2, Count: 3, Count: 4, Count: 5, Count: 6, Count: 7, Count: 8, Count: 9,
Count: 10
> Is there an elegant way of getting around this
Yes. The loop itself is not very elegant even when it works.
Although the function you shared is probably a simplified example of what you're actually doing, a much more elegant approach that works in command window, m-files, and in LiveEditor is this single line that doesn't rely on a loop.
disp(strjoin(compose('Count: %d',1:K),', '))
Count: 1, Count: 2, Count: 3, Count: 4, Count: 5, Count: 6, Count: 7, Count: 8, Count: 9, Count: 10
  2 件のコメント
John
John 2021 年 3 月 24 日
Thanks for the example. It is succinct and works consistently across different environments (at the expense of some readability). I've also submitted a bug-report for the main issue.
Adam Danz
Adam Danz 2021 年 3 月 24 日
Glad to hear it. The compose and strjoin functions are quite common but I can see how it's less readable than a simple fprintf command.

サインインしてコメントする。

その他の回答 (1 件)

Ramnarayan Krishnamurthy
Ramnarayan Krishnamurthy 2021 年 3 月 24 日
To get the same output for this example as in the command window, one possible approach is to wrap your code inside a function. Here is an example live script:
test(10)
function test(K)
% your code here
for k=1:K
fprintf('Count: %d',k);
if k < K
fprintf(","); %Add a comma for all but the last number
end
end
fprintf('\n')
end
HTH
  2 件のコメント
John
John 2021 年 3 月 24 日
Thanks for the reply. Unfortunately I'd put this under 'workaround' rather than 'solution' -- as I mentioned in my original post, you can get the intended behaviour by assembling all the output in a series of sprintf calls and then having one final fprintf, but neither solution is particularly elegant.
Ramnarayan Krishnamurthy
Ramnarayan Krishnamurthy 2021 年 3 月 24 日
I agree with you. As Stephen suggested, please create a bug report or create a support case

サインインしてコメントする。

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by