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

4 Ansichten (letzte 30 Tage)
Hello,
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'))

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 22 Dez. 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'))];
OR
xlsFiles = dir(fullfile(folderName, '*.xls*'));
[~, ~, ext] = fileparts({xlsFiles.name});
mask = ismember(ext, {'.xls', '.xlsx'});
xlsFiles = xlsFiles(mask);
  1 Kommentar
Duc Tran
Duc Tran am 22 Dez. 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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Rik
Rik am 22 Dez. 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).

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by