Operation on structure fields with common part of the field name

조회 수: 2 (최근 30일)
Romain Liechti
Romain Liechti 2019년 5월 7일
편집: Stephen23 2019년 5월 7일
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
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:
Romain Liechti
Romain Liechti 2019년 5월 7일
Yes, all the DUT have a matching REF. No particular order, the aim at the end is to have a structure of the ratio of DUT/REF with the corresponding name 1, 2 etc.
Yeah I know that's probably not the best way to do it, but I've loaded the data from my folder (all the DUT and REF files) into this structure, with the name of the file as the name of the field.
Any other idea is of course welcome

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
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
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,..."
Romain Liechti
Romain Liechti 2019년 5월 7일
Thanks a lot, it works just fine!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by