如何将 MATLAB 命令行窗口的内容保存到文件中?
我想将“命令行窗口”中的所有内容自动保存到文件中。有办法做到吗?
I want to save everything in the "Command Window" to a file automatically. Is there a way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用几个选项来保存命令行窗口中的内容:
您可以使用 DIARY 来执行此操作 命令。您甚至可以自动执行此操作,以便它始终通过修改
startup.m
文件来打开文本日志记录:
然后修改您的
finish.m
文件以将注销:这将自动存储每个 MATLAB 会话的命令行窗口的全部文本内容,这可能会变成一个相当大的文本文件。
除了使用 DIARY 命令和修改
startup.m
和finish.m
文件之外,另一个选择是使用-logfile
选项:尽管我不确定这是否会覆盖文本文件或在每次启动 MATLAB 时附加到该文件。
如果您只想保存一个或多个表达式求值的输出,可以使用 EVALC 函数用于计算包含表达式的字符串并捕获通常会以字符数组形式发送到命令窗口的输出。然后,您可以使用 FPRINTF 将此字符数组打印到文件中。
最后,如果您对保存您键入的命令的显示输出不感兴趣,而只想存储命令本身,那么命令历史记录就是您想要的。 MATLAB 会自动存储最大大小为 200,000 字节的
history.m
文件,并在添加新条目时删除最旧的条目。You have a few options available for saving content from the Command Window:
You can do this using the DIARY command. You could even automate this so that it always records what you do by modifying your
startup.m
file to turn on text logging:And then modify your
finish.m
file to turn logging off:This will automatically store the entire text content of the Command Window for every MATLAB session, which could grow into a rather large text file.
Another option besides using the DIARY command and modifying your
startup.m
andfinish.m
files is to start MATLAB using the-logfile
option:Although I'm not sure if this will overwrite the text file or append to it each time you start MATLAB.
If you're just wanting to save the output from evaluating one or more expressions, you can use the EVALC function to evaluate a string containing your expression and capture the output that would normally go to the command window in a character array. You can then print this character array to a file using FPRINTF.
Finally, if you're not interested in saving the displayed output from commands you type, but you instead just want to store the commands themselves, then the Command History is what you want. MATLAB automatically stores a
history.m
file with a maximum size of 200,000 bytes, deleting the oldest entries when newer ones are added.