Operation on structure fields with common part of the field name
3 ビュー (過去 30 日間)
古いコメントを表示
Hi, I have succesfully imported several mat files into a structure with fields names beginning either by "DUT" or "REF". Now I need to divide all the fileds beginning by "DUT" to the corresponding one beginning by "REF", for example, I want to compute DUT_2/REF_2, DUT_4/REF_4 etc...
2 件のコメント
Stephen23
2019 年 5 月 7 日
編集済み: Stephen23
2019 年 5 月 7 日
Do all of the DUT fields having a matching REF field (and vice versa) ?
Do you want the fields processed in a particular order ? (if so, what order?)
Note that this would likely be a lot simpler with a non-scalar structure, rather than awkwardly forcing meta-data (i.e. the indexing) into the fieldnames:
採用された回答
Stephen23
2019 年 5 月 7 日
編集済み: Stephen23
2019 年 5 月 7 日
S.DUT_2 = 2;
S.REF_2 = 3;
S.DUT_4 = 4;
S.REF_4 = 5;
C = fieldnames(S);
U = unique(regexp(C,'\d+$','once','match'))
F = @(n)S.(['DUT_',n]) ./ S.(['REF_',n]);
Z = cellfun(F,U)
Giving:
U =
'2'
'4'
Z =
0.66667
0.8
3 件のコメント
Stephen23
2019 年 5 月 7 日
編集済み: Stephen23
2019 年 5 月 7 日
"does this solution could work if the fields of the structure S contains vectors"
You will need to call cellfun with 'uniformoutput' set to false:
Z = cellfun(F,U, 'uni',false)
Everything else should work without change.
"I'm not familiar with anonymous function,..."
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Workspace Variables and MAT Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!