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

5 visualizzazioni (ultimi 30 giorni)
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'))

Risposta accettata

Walter Roberson
Walter Roberson il 22 Dic 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 Commento
Duc Tran
Duc Tran il 22 Dic 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!

Accedi per commentare.

Più risposte (1)

Rik
Rik il 22 Dic 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).

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by