在MATLAB GUIDE中显示大文本文件的最佳方法是什么?

时间:2011-10-26 23:51:08

标签: user-interface matlab matlab-guide

如何使用MATLAB GUIDE控件在GUI中显示文本文件的内容?文本文件可能很长或很宽,因此它应该具有垂直和水平滚动条的能力。

3 个答案:

答案 0 :(得分:8)

多行编辑框可能是显示文本的最佳选择。例如:

%# 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),如此post中所述。我也禁止编辑文本(只读)。

screenshot

答案 1 :(得分: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');

答案 2 :(得分: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);