フィルターのクリア

How to combine struct names based on the first 'n' characters of name

1 回表示 (過去 30 日間)
Kasper Buchardt
Kasper Buchardt 2020 年 11 月 11 日
編集済み: Stephen23 2020 年 11 月 11 日
I'm handling a great amount of test data, i import the filenames by using:
files = dir('*.xls');
After handling the data i end up with the 'files.name' list and a 'n x 1 matrix' which list a result:
Object1_test1.xls 3
Object1_test2.xls 6
Object2_test1.xls 4
Object3_test1.xls 5
I'm seeking a way to combine the data for each object (based on the first 'n' characters of the name) as seen below:
Object1 9
Object2 4
Object3 5

回答 (1 件)

Stephen23
Stephen23 2020 年 11 月 11 日
編集済み: Stephen23 2020 年 11 月 11 日
One approach:
fnm = {'Object1_test1.xls','Object1_test2.xls','Object2_test1.xls','Object3_test1.xls'};
val = [3,6,4,5];
tmp = regexp(fnm,'^[^_]+','once','match');
[unf,~,idx] = unique(tmp);
out = accumarray(idx,val(:))
out = 3×1
9 4 5
If you need to perform this operation for several variables, put the data into one table and use the split-apply-combine workflow.

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by