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

How can I use the MATLAB GUIDE control to display the contents of a text file in a graphical interface? A text file can be very long or very wide so that it can have vertical and horizontal scrollbars.

+3
source share
3 answers

A multi-line editbox may be the best choice for displaying 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 need to get a handle to the built-in jScrollPane java component. I use the excellent FINDJOBJ function. Then we set the HorizontalScrollBarPolicy property to javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED (= 30) as described in this post . I also turned off text editing (read-only).

screenshot

+6
source

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'); 
+1
source

Here is my solution. Good luck

 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); 
0
source

Source: https://habr.com/ru/post/900937/


All Articles