How to display long text in a GUI window, input to textscan for generic textfiles.

Hey guys,
i would like to display long text in a new GUI after pushing a pushbutton. The text is stored in a textfile. I found this code that shall create the GUI i need.
%# read text file lines as cell array of strings
fid = fopen( fullfile('C:\Users\powersyslab\Desktop\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)')
Unfortunately i get the error:
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
How can i adapt the inputs of the textscan function so that it can read generic text. Because the text i need is not formatted in a special way. It is just lines of text and numbers but in no specific order.
I would be glad for help!
Best regards, John

3 Comments

Your error says you have an invalid file identifier which implies the file does not exist in that location or could not be opened for some reason or other.
Your use of textscan looks fine to just read in lines of text without any special format.
Oha you are right, sryyy, i really chose the wrong path to my textfile:(
now i have another error occuring:
Undefined function 'findjobj' for input arguments of type 'double'.
How can that be? i took a look in the documentary of findobj but everything seems fine. Findobj just needs a handle to an object and that is given by 'hEdit'.
I dont get the mistake.
Best regards, John
If you are here you may have encountered a problem with your GUI
If you are running R2018b or later you will need to download Yair Altman's latest version of findjobj.m from
there is a bug fix for R2018b.
Regards
Terence Etchells

Sign in to comment.

 Accepted Answer

The error you're getting is nothing to do with reading generic text. It's telling you that the fid you're passing to textscan is not valid. Most likely, it's not valid because fopen failed to open the file (due to not being present, not having permission or a million other possibilities).
You could change the beginning of the code to:
[fid, errmsg] = fopen( fullfile('C:\Users\powersyslab\Desktop\license.txt'));
if fid == -1
error(errmsg);
end
In general, when dealing with files / user input, it's always a good idea to check that the input received is what you expect.

4 Comments

With regards to
Undefined function 'findjobj' for input arguments of type 'double'.
Well, yes, the proper name of the function is findobj without a 'j' in the middle.
That error message is misleading. 99% of the time, the issue is nothing to do with the type of the argument. It's that the function does not exist (not on path, misspelled, etc.)
Hello Guilaume,
thank you very much! some very stupid mistakes of mine! Unfortunately the issue isnt solved yet since a new error occured:
Attempt to reference field of non-structure array.
Error in Try1>license__Callback (line 320)
jEditbox = jEdit.getViewport().getComponent(0);
When i take a look at jEdit, i really wonder. Shouldnt that be a figure with several handles to its Properties? Instead it is empty!! Input of
jEdit = findobj(hEdit)
handlesjEdit = guidata(jEdit)
gives back this output:
jEdit =
13.0187
handlesjEdit =
[]
Sorry, i really dont get it since i am quite new to matlab GUI and i might lack some understandig of those handle structures.
I am very glad for your help to solve this issue! Best regards, John
One of two things:
- your code indeed intend to use findobj, in which case the syntax you're using is incorrect. You're only telling findobj where to search, but not what to search for.
- the code did indeed intend to use a function called findjobj, not part of matlab. Most likely, the one written by Yair, available < https://www.mathworks.co.uk/matlabcentral/fileexchange/14317-findjobj-find-java-handles-of-matlab-graphic-objects here>.
I'd lean toward the latter.

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Asked:

on 11 Sep 2015

Edited:

on 16 Nov 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!