我有一个文本文件 (.txt),我想将其全部插入 Matlab GUI 并在文本框中显示。有时,当我 GUI 打开它时,此文本框会更新,因此其中没有指定的文本。(它就像我的 GUI 的帮助说明,包含文本、数字等)。我想将整个包含此文本文件的内容自动插入到我在 Matlab GUI 中的文本框中。我应该怎么办?
谢谢。
我有一个文本文件 (.txt),我想将其全部插入 Matlab GUI 并在文本框中显示。有时,当我 GUI 打开它时,此文本框会更新,因此其中没有指定的文本。(它就像我的 GUI 的帮助说明,包含文本、数字等)。我想将整个包含此文本文件的内容自动插入到我在 Matlab GUI 中的文本框中。我应该怎么办?
谢谢。
假设文本文件的名称是file1.txt
和edit1
作为文本框的句柄,它editbox
在 的上下文中MATLAB GUIDE
,尝试在回调中插入以下代码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'
.