Asking the user to enter the extension of the files and load all of them in the current directory

1 回表示 (過去 30 日間)
Chuchu Debebe
Chuchu Debebe 2022 年 5 月 11 日
回答済み: Jan 2022 年 5 月 11 日
Dear all,
For example, I have many files(.txt, .m and others) in my directory, C:\Users\Kumsa\Desktop\OCM_MAT. I then want the user to enter his/her extension(The user may want to load either .m or .txt files in workspace) and load the files in the similar directory.. Please help me to do that.
  1 件のコメント
DGM
DGM 2022 年 5 月 11 日
If all the files are .txt files, then what's the point of asking for the extension? If you intend to support something other than .txt files, then you need to actually specify what various kinds of files you are going to support.

サインインしてコメントする。

回答 (2 件)

KSSV
KSSV 2022 年 5 月 11 日
files = dir('*.txt') ;
filenames = {files.name}

Jan
Jan 2022 年 5 月 11 日
Folder = 'C:\Users\Kumsa\Desktop\OCM_MAT';
List = dir(fullfile(Folder, '*.*'));
[~, ~, AllExt] = fileparts({List.name});
AllExt = unique(AllExt);
fprintf('Found file extensions in: %s\n', Folder);
fprintf(' %s\n', AllExt{:});
Now decide how you want to let the user choose one: by input() or in a GUI with a listbox or dropdown menu?
Imprting the files works like this:
List = dir(fullfile(Folder, ['*', selectedExt]));
Files = fullfile(Folder, {List.name});
for k = 1:numel(Files)
% import Files{k}...
end

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by