複数のcsvファイルを一度に読み込みたい
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
同じフォルダに1stcap01.csv, 1stcap02.csv,1stcap03.csv . . . と複数のファイルがある時に
以下の処理を用いて、それぞれのデータの読み込みを同時に行いたいです。
1つ1つ処理をすれば良いのですが、csvファイルの数が多いため、何か良い方法がありましたらご教示いただけると幸いです。
data = readmatrix('1stcap01.csv');
idx = isnan(data(:,2));
idx_or =[0;diff(idx)]<0|[diff(idx);0]>0;
num = find(idx_or);
if mod(length(num),2)
num = [num;height(idx_or)];
end
for ii = 1:length(num)/2
A{ii,1} = data(num(2*ii-1):num(2*ii),:);
end
B = cellfun(@(x) rmmissing(x,2),A,'UniformOutput',false);
cellfun(@size,B,'UniformOutput',false)
よろしくお願いいたします。
0 commentaires
Réponse acceptée
Hernia Baby
le 23 Mar 2022
関数にして繰り返し操作し、連結させましょう。
■フルパス取得
clc,clear;
Pathlists = dir("*.csv");
Mylists = (struct2cell(Pathlists))';
Mylists = string(Mylists(:,1:2));
MyPath = fullfile(Mylists(:,2),Mylists(:,1))
中身を少しだけ変えたものを入れています
■関数の使用
MyPreprocessという関数を作り、縦に連結させています
MyCell = [];
for jj = 1:height(MyPath)
MyCell = [MyCell;MyPreprocess(MyPath(jj))];
end
MyCell
どこでどうやって区切ったかわからない場合は以下のようにするといいと思います
MyCell = [];
for jj = 1:height(MyPath)
MyCell = [MyCell;{MyPath(jj)};MyPreprocess(MyPath(jj))];
end
MyCell
■以下関数
今回は同じスクリプト内に以下の用にコード化しました
パスからcsvを読み込んで処理しているだけです
function B = MyPreprocess(path)
data = readmatrix(path);
idx = isnan(data(:,2));
idx_or =[0;diff(idx)]<0|[diff(idx);0]>0;
num = find(idx_or);
if mod(length(num),2)
num = [num;height(idx_or)];
end
for ii = 1:length(num)/2
A{ii,1} = data(num(2*ii-1):num(2*ii),:);
end
B = cellfun(@(x) rmmissing(x,2),A,'UniformOutput',false);
end
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur スプレッドシート dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!