how to get files in a folder to a cell format

22 ビュー (過去 30 日間)
vishnu vardhan reddy
vishnu vardhan reddy 2023 年 4 月 17 日
移動済み: Stephen23 2025 年 6 月 3 日
i have files in a folder which i need to make a cell which contains entire file name path
for example
my folder contains 1.mat, 2.mat, 3.mat files
how should i loop them to get into this form { 1.mat, 2.mat, 3.mat }
  1 件のコメント
Stephen23
Stephen23 2023 年 4 月 17 日
移動済み: Stephen23 2025 年 6 月 3 日
The simple MATLAB approach is to use a comma-separated list. To avoid the dot-directory names and any other files and folders, it is strongly recommended to specify a filename including wildcard and extension:
F = 'path_of_your_folder';
S = dir(fullfile(F,'*.mat'));
C = {S.name} % much simpler and more efficient than your loop

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

採用された回答

Frederic Rudawski
Frederic Rudawski 2023 年 4 月 17 日
編集済み: Frederic Rudawski 2023 年 4 月 17 日
Something like that should work:
folder = 'path_of_your_folder/'
data = dir(folder);
idx = 1;
for n = 1:length(data)
if ~data(n).isdir
filename{idx} = [folder data{n}.name]
idx = idx+1;
end
end
  3 件のコメント
Murthy
Murthy 2025 年 6 月 2 日
Fantastic answer. It worked like a charm. Thanks.
Walter Roberson
Walter Roberson 2025 年 6 月 2 日
F = 'path_of_your_folder';
S = dir(fullfile(F,'*.mat'));
C = fullfile({S.folder}, {S.name});
as you want a cell containing the entire path name.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2023 年 4 月 17 日
Try this:
% Process a sequence of files.
folder = pwd; % Or wherever you want.
filePattern = fullfile(folder, '*.mat');
fileList = dir(filePattern) % Create a structure array.
% Get all filenames into one cell array. Filenames have the complete path (folder prepended).
allFileNames = fullfile(folder, {fileList.name});
% Loop over all filenames to process them somehow.
numFiles = numel(allFileNames);
for k = 1 : numel(allFileNames)
% Get this file name.
fullFileName = allFileNames{k};
fprintf('Processing %s\n', fullFileName);
% Now do something with fullFileName, such as passing it to load.
end
  1 件のコメント
Image Analyst
Image Analyst 2023 年 4 月 17 日
Glad it worked for you.
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by