matlabuser-interfacetext-filesmatlab-guidetextreader

How to import the entire content of a text file into Edit Text Box of Matlab GUI?


I have a text file (.txt) that I want insert all containing of it to Matlab GUI and show it in a text box. Sometimes this text box updates when I GUI open it so there isn’t a specified text in it. (It is like a help note for my GUI that has text, numbers etc.) . I want insert whole containing of this text file automatically to my text box in Matlab GUI. What should I do?

Thanks.


Solution

  • Assuming the name of the text file as file1.txt and edit1 as the handles to the text-box, which is editbox in the context of MATLAB GUIDE, try inserting the following code inside the OpeningFcn callback or with some GUI component callback -

    data1 = importdata('file1.txt','') %// Import all text as a cell array with each cell for each line
    set(handles.edit1, 'Max', 2); %// Enable multi-line string input to the editbox
    set(handles.edit1,'String',data1) %//  Put the text from text file into the editbox using `String` property
    set(handles.edit1,'HorizontalAlignment','left') %// Align the text left-based, as it looks good maybe, but feel free to change it to center or right
    

    If the text file is not present in the current directory, provide absolute path to it instead of 'file1.txt'.