Extracting strings between different characters from a cell array without loop
古いコメントを表示
Hello,
I would like to rewrite the code below to remove the loop. I assume I need to use cellfun but after several attempts I am not managing.
In the following cell array, I need to extract the characters between "LS=" and "DS=", between "DS=" and "CS=", and between "CS=" and "]" and save them in their corresponsing field in a table.
This is how my orgiginal cell array looks like (I've cut it to 3 rows but there are actually a lot more in my dataset):
commentO =
3×1 cell array
{'LS=Students201509DS=Teachers201509CS=ConfigVS=English]' }
{'LS=Preschoolers201801DS=Students201910CS=AbsVS=Italian]' }
{'LS=Preschoolers201902DS=Assistants201902CS=NAVS=Italian]'}
This is the code I have written to extract the strings I need and store them in their corresponding field in a table:
commentO = {'LS=Students201509DS=Teachers201509CS=ConfigVS=English]'; 'LS=Preschoolers201801DS=Students201910CS=AbsVS=Italian]'; 'LS=Preschoolers201902DS=Assistants201902CS=NAVS=Italian]'};
MyDataTable = table;
MyDataTable.LS = cell(size(commentO));
MyDataTable.DS = cell(size(commentO));
MyDataTable.CS = cell(size(commentO));
MyDataTable.VS = cell(size(commentO));
for i = 1:length(commentO)
tempStr = commentO{i,1};
MyDataTable.LS{i} = tempStr(strfind(tempStr, 'LS=')+3:(strfind(tempStr, 'DS=')-1));
MyDataTable.DS{i} = tempStr(strfind(tempStr, 'DS=')+3:(strfind(tempStr, 'CS=')-1));
MyDataTable.CS{i} = tempStr(strfind(tempStr, 'CS=')+3:(strfind(tempStr, 'VS=')-1));
MyDataTable.VS{i} = tempStr(strfind(tempStr, 'VS=')+3:(end-1));
end
It takes ages so if someone could help me out with the correct cellfun call to do this much more efficiently. I would be very grateful.
Thank you very much.
Best Regards;
Cecile
1 件のコメント
dpb
2020 年 1 月 4 日
cellfun alone is unlikely to make much difference performance-wise--it's just a loop under the hood.
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Cell Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!