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

3 次查看(过去 30 天)
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'))

采纳的回答

Walter Roberson
Walter Roberson 2021-12-22
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 个评论
Duc Tran
Duc Tran 2021-12-22
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!

请先登录,再进行评论。

更多回答(1 个)

Rik
Rik 2021-12-22
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).

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by