MATLAB Answers

How do I display list of folder (as text file)

9 views (last 30 days)
Hello, I'm trying to make a program so that it can make a list of file(s) in a folder and display them in the Command Window so the user can see it.
I have tried to use dir function to get the list of files but I don't know how to make it into something that can be displayed. I don't know how to go from here so this is the only thing I have so far:
a = dir;
a.name %I got all the files name on this step
I'm trying to achieve something like this in the command window:
>>Existing files: %I'm trying to display this list, which varies depends on the files in the folder
- a.m
- b.txt
Choose file to load:
>> <User input>
Thank you

  0 Comments

Sign in to comment.

Accepted Answer

David Fletcher
David Fletcher on 7 May 2018
files=dir
sprintf('%s \n',files.name)
fName=input('choose file:','s')

  4 Comments

Show 1 older comment
David Fletcher
David Fletcher on 7 May 2018
A simple 'bodge' would be to delete the first two entries of the directory list.
files(1:2)=[]
However, be aware that I call this a 'bodge' for a reason - the current and parent directory entries may not always be the first two items in the file list. In most cases they will be, but if you have some weird file names (i.e. names starting with a character values less than '.'), then they won't be). A complete solution would be to actively search for the . and .. entries in the list and remove them.
David Fletcher
David Fletcher on 7 May 2018
After a bit of searching and testing you can use
files=files(~ismember({files.name},{'.','..'}))
to remove the offending entries

Sign in to comment.

More Answers (0)

Sign in to answer this question.