さまざまなフォルダーやテキスト ファイルからデータを matlab にインポートすることを目指しています。
clear all
main_folder = 'E:\data';
%Directory of data
TopFolder = dir(main_folder);
%exclude the first two cells as they are just pointers.
TopFolder = TopFolder(3:end);
TopFolder = struct2cell(TopFolder);
Name1 = TopFolder(1,:);
%obtain the name of each folder
dirListing = cellfun(@(x)dir(fullfile(main_folder,x,'*.txt')),Name1,'un',0);
Variables = cellfun(@(x)struct2cell(x),dirListing,'un',0);
FilesToRead = cellfun(@(x)x(1,:),Variables,'un',0);
%obtain the name of each text file in each folder
これにより、「main_folder」内の各フォルダー内の各テキスト ファイルの名前が提供されます。現在、for ループを使用せずにデータを読み込もうとしています (これを行うには for ループの方が速い場合があることは認識していますが、コンパクトなスクリプトを目指しています)。
for ループで使用する方法は次のとおりです。
for k = 1:length(FilesToRead);
filename{k} = cellfun(@(x)fullfile(main_folder,Name{k},x),FilesToRead{k},'un',0);
fid{k} = cellfun(@(x)fopen(x),filename{k},'un',0);
C{k} = cellfun(@(x)textscan(x,'%f'),fid{k},'un',0);
end
ループをまったく使用しない方法はありますか? おそらくcellfun内のcellfunのようなものですか?