datastoreを途中から読み込む方法

4 views (last 30 days)
shingo maruyama
shingo maruyama on 12 Mar 2024
Commented: shingo maruyama on 18 Mar 2024
複数のcsvファイルからdatastoreを作成したとき、途中から読み込む方法はありますか?
read関数の説明では、resetして始めから読み込む方法しか書かれてなく時間がかかります。
画像ファイルの時のreadimage関数のように何番目を指定して読む方法はないでしょうか。
いい方法がありましたら教えていただけると幸いです。
  2 Comments
Kojiro Saito
Kojiro Saito on 14 Mar 2024
データストアのReadSizeによって手順が異なりますが、現在はfile (ファイル単位)になっているか20000とかの数値になっているかどちらでしょうか?前者のファイル単位での読み取りで良ければストレートなやり方があり、後者だとちょっと工夫が必要です。
shingo maruyama
shingo maruyama on 14 Mar 2024
回答ありがとうございます。
データストアのReadSizeですがデフォルトのまま20000になっていますが、各csvファイルの行数は1000程度なので、ファイル単位で読み込んでいます。ですので、前者のはずです。
よろしくお願いします。

Sign in to comment.

Accepted Answer

Kojiro Saito
Kojiro Saito on 15 Mar 2024
CSV で使われる表形式データストア(tabularTextDatastore)がサポートしているpartitionで指定した位置のデータを抽出できます。ただ、位置(下記コードの変数index)は1つしか指定できないのでx番目以降をまとめて取得したい場合はindexの値を変えて繰り返し実施する必要があります。
ttds = tabularTextDatastore("*.csv");
% 分割数を取得
n = numpartitions(ttds);
% データストアを分割して4番目を取得
index = 4;
subds = partition(ttds, n, index);
% 当該データを読み込む
while hasdata(subds)
data = read(subds);
end
また、datastoreのサブセットを作れるsubsetコマンドがあるのでこれが使えるmatlab.io.datastore.FileSetのタイプでデータストアを作成することでも実現可能です。subsetでは指定する位置を複数入れられるので、3番目以降とするときは3:maxpartitions(fs)のように指定できます。
fs = matlab.io.datastore.FileSet("*.csv");
% 分割数を取得
n = maxpartitions(fs);
% 3番目以降の分割を取得
subds = subset(fs, 3:n);
while hasNextFile(subds)
% ファイル情報を取得
file = nextfile(subds);
% テーブルとして読み取り
t = readtable(file.Filename);
% 何かしらの処理
end
  1 Comment
shingo maruyama
shingo maruyama on 18 Mar 2024
回答ありがとうございます。
おかげさまで、希望通り、途中から読み込むことができるようになりました。

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!