How to add .txt files from directory to a cell array

1 回表示 (過去 30 日間)
EL
EL 2019 年 8 月 30 日
回答済み: Walter Roberson 2019 年 8 月 31 日
Hey guys,
How do I load every file in the directory in numerical/alphabetical order to a cell array?
prompt = 'Select your files to load';
filedir=uigetdir('Y:\ProgramFiles\Data\')
From here, I have a directory selected, how would I load every file in there into a cell array?
I plan on using the cell array in a loop, where each file name would be loaded in order and run through another script as part of a function

回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 8 月 31 日
dinfo = dir( fullfile(filedir, '*.txt'));
filenames = {dinfo.name}; %no directory information
filenames = nat_sort(filenames); %from file exchange
fullnames = fullfile(filedir, filenames);
numfiles = length(fullnames);
results = cell(numfiles, 1);
for K = 1 : numfiles
thisfile = filenames{K};
now load file designated by thisfile and process it
results{K} = output for this file
end

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by