inserting contents of text file into a Listbox

6 views (last 30 days)
Hello. I have a text file that has a list of images that need processing (crop them and resave). So I am wanting to open the text file, and display the contents (=list of image files to be cropped) into a listbox. I can open the text file OK, but then I can't get its contents into the listbox:
while ischar(tline)
tline = (fgetl(fid1))
set(handles.listbox,'String',{tline})
end
fclose(fid1);
Jason
  3 Comments
Orion
Orion on 31 Oct 2014
Edited: Orion on 31 Oct 2014
Note : your method will take more time because you are updating the listbox at every iteration of the loop (long list -> long time), so you just should put the update line outside the loop.
str = [];
i = 0;
while ~feof(fid)
i = i+1;
str{i} = fgetl(fid);
end
set(handles.listbox1,'String',str);

Sign in to comment.

Accepted Answer

Orion
Orion on 31 Oct 2014
Hi,
I guess you need this :
% Read the list in the text file "TextFile.txt"
fid = fopen('TextFile.txt','r');
MyList = textscan(fid,'%s','delimiter','\n');
fclose(fid);
% put the list in the listbox
set(handles.listbox1,'String',MyList{:});

More Answers (0)

Categories

Find more on Environment and Settings 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!