Where can I find the directory for things like '*.xls', '%04df' or something?

1 view (last 30 days)
Duc Tran
Duc Tran on 22 Dec 2021
Commented: Duc Tran on 22 Dec 2021
I am self teaching myself MATLAB so my apology if this is a very basic question. I want to use the function 'dir' to go into a folder and make a struct with only those files ending in '.xls' or '.xlsx'. Below is what I currently have and I would like to know how I can make it to also list '.xlsx' file in the same struct.
In addition, I would like to know where I can go to understand the nomenclature? of words like '%04df' and others.
xlsFiles = dir(fullfile(folderName, '*.xls'))

Accepted Answer

Walter Roberson
Walter Roberson on 22 Dec 2021
xlsFiles = dir(fullfile(folderName, '*.xls*'))
should get .xls and .xlxs files. But it would also get .xlsm and any other file extension that started with .xls .
To be specific about xlsx and xls and you want them in the same structure, there are two ways:
xlsFiles = [dir(fullfile(folderName, '*.xls'));
dir(fullfile(folderName, '*.xlsx'))];
xlsFiles = dir(fullfile(folderName, '*.xls*'));
[~, ~, ext] = fileparts({xlsFiles.name});
mask = ismember(ext, {'.xls', '.xlsx'});
xlsFiles = xlsFiles(mask);
  1 Comment
Duc Tran
Duc Tran on 22 Dec 2021
This works really well, thank you so much for bringing up how I can avoid getting .xlsm and others as well I didn't account for it!

Sign in to comment.

More Answers (1)

Rik on 22 Dec 2021
I would personally go the easy route and merge the two separate structs, but you could try this:
xlsFiles = dir(fullfile(folderName, '*.xls*'));
As for '%04d', that look like a format specification. You should read the documentation for fprintf or sprintf (although num2str supports this as well).



Community Treasure Hunt

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

Start Hunting!