I am using the following code to read all files from a folder:
Files=dir('*');
for k=1:length(Files)
FileNames=Files(k).name;
end
However I want to read the files which names begin with "word" not just all files in folder. How can I perform such task?

 Akzeptierte Antwort

Image Analyst
Image Analyst am 1 Jul. 2014

4 Stimmen

Files=dir('word*.*');

7 Kommentare

Reema Alhassan
Reema Alhassan am 24 Mai 2018
hey but here we didn't specify the folder name is that ok?
Yes, it will use files in the current folder. If you want them in a particular folder, you can do
folder = pwd; % or 'D:\my Images\Reema' or wherever...
filePattern = fullfile(folder, 'word*.*');
fileList = dir(filePattern);
Hassan Mehmood Khan
Hassan Mehmood Khan am 25 Jul. 2020
Thank you
it was very helpful
sania urooj
sania urooj am 13 Feb. 2021
hey! I want to read files (mat) also, the name of the folder (label) . What to do?
Image Analyst
Image Analyst am 13 Feb. 2021
@sania urooj, I'm not sure what names you have and what you want. Why can't you just change the filePattern wildcard to be what you want???
Ihaveaquest
Ihaveaquest am 16 Aug. 2022
what if there are multiple files with similar name but you only want the most current created ??
@Ihaveaquest, to find the newest file, do this:
fileList = dir('t*.m')
fileDates = [fileList.datenum]
% Find newest file
[maxDate, index] = max(fileDates)
fprintf('The newest file is:\nName: %s\nDate: %s\n', ...
fileList(index).name, fileList(index).date)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by