显示来自 MATLAB 的信息,无需换行

发布于 2024-07-25 02:21:06 字数 334 浏览 9 评论 0原文

有没有办法在没有结束换行的情况下输出/显示 MATLAB 程序中的信息?

我的 MATLAB 程序时不时地输出一个数字。 在输出数字之间,程序还执行许多其他操作。 这是一个主要用于指示某种进度的构造,最好不要每次都换行,只是为了让用户更容易阅读。 这大约就是我正在寻找的:

Current random seed:
4 7 1 1 

如果程序仍然执行与以前相同的操作,则程序的下一个输出将位于同一行。

我已经阅读了有关 disp、sprintf 和 format 的文档,但没有找到我要找的内容。 这并不意味着它不存在。 ;)

Is there any way to output/display information from a MATLAB program without an ending line feed?

My MATLAB program outputs a number a bit now and then. Between outputting the number the program does a lot of other stuff. This is a construct mainly to indicate some kind of progress and it would be nice not to have a line feed each time, just to make it more readable for the user. This is approximately what I'm looking for:

Current random seed:
4 7 1 1 

The next output from the program would be on the same row if it is still doing the same thing as before.

I've read the doc on disp, sprintf, and format but haven't found what I'm looking for. This doesn't mean it isn't there. ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屋檐 2024-08-01 02:21:06

除非您明确指示,否则 fprintf 函数不会添加换行符。 省略 fid 参数以将其打印到命令行窗口。

fprintf('Doing stuff... ');
for i = 1:5
    fprintf('%d ', i);
    % do some work on that pass...
end
fprintf(' done.\n'); % That \n explicitly adds the linefeed

使用 sprintf 不太有效:它创建一个没有换行符的字符串,但是如果您使用 disp() 或省略分号,disp 自己的显示逻辑将添加换行符。

The fprintf function does not add a line feed unless you explicitly tell it to. Omit the fid argument to have it print to the Command Window.

fprintf('Doing stuff... ');
for i = 1:5
    fprintf('%d ', i);
    % do some work on that pass...
end
fprintf(' done.\n'); % That \n explicitly adds the linefeed

Using sprintf won't quite work: it creates a string without a line feed, but then if you use disp() or omit the semicolon, disp's own display logic will add a line feed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文