How Can I Show a txt File in a GUI ?

20 views (last 30 days)
I have a 5 txt file about informations of 5 different people . I designed a drop down menu about 5 people in the guı as you can see in the figure.For instance when I click to person 1 at the drop down it will show the first txt file. Should I put Edit field or text area into a guı to show the text files? How can I show these text files in the GUI ?

Accepted Answer

Jorg Woehl
Jorg Woehl on 5 Mar 2021
A text area would be the way to go, as it will show a scroll bar and wrap text if the content of the textfile is larger than the space available. Just make sure that you make it non-editable in App Designer (Inspector > Interactivity) if you only want to show the text but not allow the user to edit it.
I suppose you have one textfile per person. In this case, the callback for the dropdown menu could look similar to this:
value = app.DropDown.Value;
% choose the correct textfile depending on which person was selected
switch value
case 'Option 1'
myfile = 'person1.txt';
case 'Option 2'
% open the file and display its contents in the text area
fileID = fopen(myfile);
C = textscan(fileID, '%s', 'Delimiter', '\n');
app.TextArea.Value = C{1};
To get to the callback, right-click on the dropdown menu and select Callbacks > DropDownValueChanged callback.
Note that you will have to adjust the callback code if you change the names of the options in the dropdown menu (Option 1, Option 2, etc.).

More Answers (0)


Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by