Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to display a large text file in MATLAB GUIDE?

How can a MATLAB GUIDE control be used to display the contents of a text file in a GUI? The text file may be very long or very wide so it should have the ability to have vertical and horizontal scroll bars.

like image 376
julianfperez Avatar asked Dec 05 '25 10:12

julianfperez


2 Answers

A multi-line editbox may be the best choice to display the text. Example:

%# 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)')

To enable horizontal scrolling, we must get a handle to the embedded JScrollPane java component. I am using the excellent FINDJOBJ function. Then we set the HorizontalScrollBarPolicy property to javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED (= 30) as explained in this post. I also disabled editing of the text (read only).

screenshot

like image 129
Amro Avatar answered Dec 07 '25 23:12

Amro


Here is my solution for a generic text file called "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');
like image 21
julianfperez Avatar answered Dec 07 '25 23:12

julianfperez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!