how to load from text file to listbox

7 views (last 30 days)
Norah Mohammed
Norah Mohammed on 3 Jul 2013
Answered: Image Analyst on 8 Apr 2025
Hello,
I'm using GUI Guide in the MATLAB:
How to load a text file (that contain numbers) when clicking on the pushbutton and then display the text file contents in my listBox
I want to display the numeric on the text file as two columns in the ListBox, for example:
123 456
897 127
164 865
and I tried this code:
function load_Callback(hObject, eventdata, handles)
% hObject handle to load (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename pathname] = uigetfile('*.txt', 'File Selector]');
fullpathname=strcat(pathname , filename);
x = fileread(fullpathname);
set(handles.badPixelList,'string',x);
but the results on my ListBox looks like this:
123456897127164865
Thank you.

Answers (2)

Tridib
Tridib on 8 Apr 2025
After creating a new GUI with a “Push Button” and a “List Box” using Guide, in the “.m” file generated by GUIDE, make the following changes to the function named “pushbutton1_Callback”:
function pushbutton1_Callback(hobject, eventdata, handles)
% hobject handle to pushbutton1 (see GCBO)
% eventdata reserved to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% "uigetfile" function lets the user select a text file
[file, path] = uigetfile('*.txt', 'Select a Text File');
% Check if user canceled
if isequal(file, 0)
errordlg('No file selected!', 'File Error');
return;
end
% Read the file data
filename = fullfile(path, file);
fileData = load(filename);
% Check if data is numeric and in two columns
if ~isnumeric(fileData) || size(fileData, 2) ~= 2
errordlg('File must contain numeric data with two columns!', 'Data Error');
return;
end
% Format data as two columns for ListBox
% This loops through the data row by row, using
%sprintf to create a string with spacing between columns
formattedData = cell(size(fileData, 1), 1);
for i = 1:size(fileData, 1)
formattedData{i} = sprintf('%d %d', fileData(i, 1), fileData(i, 2));
end
% "set" updates the ListBox to display the formatted content
set(handles.listbox1, 'String', formattedData);
Save the “.fig” and “.m’ files and run to test the functionality. Also, here, it is assumed that the text file contains the numeric data in two columns only, like:
123 456
897 127
164 865
For more information on the “uigetfile” and “set” function, refer to the following documentations:
As GUIDE has been deprecated from MATLAB R2021a, it is recommended to use App Designer for the latest releases.
Hope this helps!

Image Analyst
Image Analyst on 8 Apr 2025
You can use readlines to read the lines of the text file and then put them into the listbox. For App Designer, it would be something like this (untested)
linesInFile = readlines(filename);
app.listbox.Items = linesInFile;

Categories

Find more on Desktop 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!