【问题标题】:How to import the entire content of a text file into Edit Text Box of Matlab GUI?如何将文本文件的全部内容导入 Matlab GUI 的编辑文本框?
【发布时间】:2014-07-16 12:23:08
【问题描述】:

我有一个文本文件 (.txt),我想将它的所有内容插入 Matlab GUI 并在文本框中显示。有时这个文本框会在我 GUI 打开它时更新,因此其中没有指定的文本。 (它就像我的 GUI 的帮助说明,包含文本、数字等)。我想将整个包含此文本文件的内容自动插入到我在 Matlab GUI 中的文本框中。我该怎么办?

谢谢。

【问题讨论】:

  • 您是否为此使用 MATLAB GUIDE?
  • 是的。我正在使用 Matlab 指南

标签: matlab user-interface text-files matlab-guide textreader


【解决方案1】:

假设文本文件的名称为file1.txtedit1 作为文本框的句柄,在MATLAB GUIDE 的上下文中为editbox,尝试在OpeningFcn 中插入以下代码回调或带有一些 GUI 组件回调 -

data1 = importdata('file1.txt','') %// Import all text as a cell array with each cell for each line
set(handles.edit1, 'Max', 2); %// Enable multi-line string input to the editbox
set(handles.edit1,'String',data1) %//  Put the text from text file into the editbox using `String` property
set(handles.edit1,'HorizontalAlignment','left') %// Align the text left-based, as it looks good maybe, but feel free to change it to center or right

如果当前目录中不存在该文本文件,请提供其绝对路径而不是'file1.txt'

【讨论】:

  • 感谢您的完美回答。
  • @user2991243 非常欢迎!在这个过程中我自己学到了一些关于编辑框的东西:)
  • @user2991243 您也可能会发现其他相关问题 - stackoverflow.com/questions/23759822/…
  • 再次感谢您的帮助 :-)
猜你喜欢
  • 1970-01-01
  • 2016-10-12
  • 2014-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-17
  • 2018-04-24
  • 1970-01-01
相关资源
最近更新 更多