Open multiple *.tsv files

4 ビュー (過去 30 日間)
Mário Sobrinho
Mário Sobrinho 2021 年 3 月 24 日
回答済み: Mário Sobrinho 2021 年 3 月 26 日
Hello to all,
I'm quite new in Matlab. How can I open multiple *.tsv files stored in one folder? I have several files ordered by date and I want to open some or all of them to create a chat with the data evolution with time. Right now I'm openning one by one but I guess this can be done automatically.
Thanks

採用された回答

Kojiro Saito
Kojiro Saito 2021 年 3 月 26 日
datastore might fit your case.
If your PC has enought memory (RAM) to store all tsv files in workspace at once, you can use readall function.
For example, suppose tsv files are located in data directory and each tsv has column name col1, you can calculate the mean by the following.
ds = datastore('data/*.tsv', 'Type', 'tabulartext', 'FileExtensions', '.tsv');
t = readall(ds);
m = mean(t.col1);
Or, if the RAM is not sufficient, you can read by a chunk.
ds = datastore('data/*.tsv', 'Type', 'tabulartext', 'FileExtensions', '.tsv');
sums = [];
counts = [];
while hasdata(ds)
t = read(ds);
sums(end+1) = sum(t.col1);
counts(end+1) = length(t.col1);
end
m = sum(sums)/sum(counts);
alternatively, you can also use tall array.
ds = datastore('data/*.tsv', 'Type', 'tabulartext', 'FileExtensions', '.tsv');
tt = tall(ds);
m = gather(mean(tt.col1));

その他の回答 (1 件)

Mário Sobrinho
Mário Sobrinho 2021 年 3 月 26 日
Hi Kojiro,
Thank you for your answer.

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by