如何使用 MATLAB GUIDE 控件在 GUI 中显示文本文件的内容?文本文件可能很长或很宽,因此它应该具有垂直和水平滚动条的能力。
问问题
7822 次
3 回答
9
多行编辑框可能是显示文本的最佳选择。例子:
%# read text file lines as cell array of strings
fid = fopen( fullfile(matlabroot,'license.txt') );
str = textscan(fid, '%s', 'Delimiter','\n'); str = str{1};
fclose(fid);
%# GUI with multi-line editbox
hFig = figure('Menubar','none', 'Toolbar','none');
hPan = uipanel(hFig, 'Title','Display window', ...
'Units','normalized', 'Position',[0.05 0.05 0.9 0.9]);
hEdit = uicontrol(hPan, 'Style','edit', 'FontSize',9, ...
'Min',0, 'Max',2, 'HorizontalAlignment','left', ...
'Units','normalized', 'Position',[0 0 1 1], ...
'String',str);
%# enable horizontal scrolling
jEdit = findjobj(hEdit);
jEditbox = jEdit.getViewport().getComponent(0);
jEditbox.setWrapping(false); %# turn off word-wrapping
jEditbox.setEditable(false); %# non-editable
set(jEdit,'HorizontalScrollBarPolicy',30); %# HORIZONTAL_SCROLLBAR_AS_NEEDED
%# maintain horizontal scrollbar policy which reverts back on component resize
hjEdit = handle(jEdit,'CallbackProperties');
set(hjEdit, 'ComponentResizedCallback',...
'set(gcbo,''HorizontalScrollBarPolicy'',30)')
要启用水平滚动,我们必须获得嵌入式 JScrollPane java 组件的句柄。我正在使用出色的FINDJOBJ功能。然后我们将HorizontalScrollBarPolicy
属性设置为javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
(= 30),如本文所述。我还禁用了文本的编辑(只读)。
于 2011-10-28T17:43:56.217 回答
1
这是我对名为“textfile.txt”的通用文本文件的解决方案:
f = figure('menu','none','toolbar','none');
fid = fopen('textfile.txt');
ph = uipanel(f,'Units','normalized','position',[0.4 0.3 0.5 0.5],'title',...
'Display window');
lbh = uicontrol(ph,'style','listbox','Units','normalized','position',...
[0 0 1 1],'FontSize',9);
indic = 1;
while 1
tline = fgetl(fid);
if ~ischar(tline),
break
end
strings{indic}=tline;
indic = indic + 1;
end
fclose(fid);
set(lbh,'string',strings);
set(lbh,'Value',1);
set(lbh,'Selected','on');
于 2011-10-28T11:26:25.587 回答
0
这是我的解决方案。祝你好运
fid = fopen(filename);
str = textscan(fid, '%s', 'Delimiter','\n'); str = str{1};
fclose(fid);
f=figure;
hPan = uipanel(f,'Units','normalized');
uicontrol(hPan, 'Style','listbox', ...
'HorizontalAlignment','left', ...
'Units','normalized', 'Position',[0 0 1 1], ...
'String',str);
于 2014-11-27T12:17:05.570 回答