Reading files with different extension from directory

Currently I am reading files from a directory using "fullName = dir([path '*.tif'])" to find all files with the .tif extension. If the folder contains .png files instead, I have to change the code to "fullName = dir([path '*.png'])". Is there a way to do this without changing the code i.e. is there a way to read the image file from a folder that contains either .png or .tif files without changing the code?

 Accepted Answer

The MATLAB builtin dir() function doesn't have the facility to handle multiple extensions in a single call; there are a couple of ways to approach it
  1. dir() of the subdirectory of interest for all files and then select those for which the file extension is one of your select list. This entails returning fileparts of the dir() struct to be able to scan the extensions directly to avoid the possible matching of some substring in the file name.
  2. Make multiple calls to dir() with each of the desired file extensions in turn and combine the resulting structures to get the overall.
There are various perturbations about the above, but that's the basic choice without using direct calls to the OS which may have more extension features available.

7 Comments

2.
S = [...
dir(fullfile(P,'*.tif'));...
dir(fullfile(P,'*.png'))];
Using the following code,
path = 'C:\Users\data\2X 600 0221 02\'; addpath(path);
s = [...
fullName = dir([path '*.tif']);...
fullName = dir([path '*.png'])];
I get this following error
Error: File: Base.m Line: 5 Column: 14
Incorrect use of '=' operator. To compare values for equality, use '=='. To specify name value arguments, check that name is a
valid identifier with no quotes.
@Jiayun Liu: why did you add
fullName =
into the code I showed you?
Why are you calling ADDPATH? There does not seem to be any point to that.
Oh yes I understand now. Thanks.
My matlab file is in a different location as my data file. The way I understand, dir searches within the current .m file location so I add the data file location. Is there a better way to do this?
"The way I understand, dir searches within the current .m file location..."
No: if you provide it with a path then it will search that path. Otherwise "dir lists files and folders in the current folder". The DIR documentation does not mention anything about the current Mfile location.
"... so I add the data file location. Is there a better way to do this?"
Yes: the better way is to use an absolute/relative path, just as I showed you.
ADDPATH is completely superfluous and should not be used for accessing data files.
Just for rounding out...
1.
EXT={'.tif','.png'}; % the list of extensions wanted
D=dir(fullfile(P,'*.*')); % directory of the path with all extensions
[~,~,e]=fileparts({D.name}); % the list of extensions found
D=D(matches(e,EXT,"IgnoreCase",1)); % save the matches
In the end, it's probably easier to just suffer the overhead of the multiple calls for just a couple in the list; if the list grows significantly it might be better to do something like this. In this case one only modifies the one data list of possible extensions; in the other have to keep adding lines of code.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 12 Oct 2022

Commented:

dpb
on 12 Oct 2022

Community Treasure Hunt

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

Start Hunting!