Loop through specific strcuture field names
이전 댓글 표시
I would like to loop through specitifc feild names of my struct. At the moment the field names are typed out individually for each entry in "names" like this (variable names are more descriptive in the real code):
names={'di','da','du','bi','ba','bo','fi','fa','fu','fe'};
[R{i}.di_Beat,R{i}.di_R_med,R{i}.di_D_med,R{i}.di_R_m,...
R{i}.di_D_m]=Cut(L{i}.di_F,S_start{i},S_length{i},ts{i},1,'g');
Currently I just rewrote these two lines of code for each entry of name, this would be the next two lines for example:
[R{i}.da_Beat,R{i}.da_R_med,R{i}.da_D_med,R{i}.da_R_m,...
R{i}.da_D_m]=Cut(L{i}.da_F,S_start{i},S_length{i},ts{i},1,'g');
The field names do not appear all one after another so i can just pull out the names. I tried to concatenate teh variable names as a string unsing strcat but that did not work out. I did something along the linnes of:
for n=1:length(names)
Beat=strcat('R{i}.',names{n},'_Beat');
R_med=strcat('R{i}.',names{n},'_R_med');
D_med=strcat('R{i}.',names{n},'_D_med');
R_m=strcat('R{i}.',names{n},'_R_m');
D_m=strcat('R{i}.',names{n},'_D_m');
Signal=strcat('L{i}.',names{n},'_F');
[Beats,R_med, D_med, R_m, D_m]=Cut(Signal,S_start{i},S_length{i},ts{i},1,'g');
end
This still seems too compliocated and I am looking for an easier way to do this.
Any help is highly appreciated!
댓글 수: 3
It seems that you are putting some kind of meta-data into the fieldnames, and are now starting to find out why that is a bad way to write code. Putting meta-data into fieldnames (or variable names) makes code complex, slow, and buggy. Read this to know why:
As far as I can tell, your code would be much simpler if you replaced the fieldnames (with meta-data) and the non-scalar cell array with one non-scalar structure:
S(r,c).Beat = ...
S(r,c).Rmed = ...
S(r,c).Dmed = ...
...
S(r,c).meta = ... di/da/du/...
SH
2019년 3월 22일
Jos (10584)
2019년 3월 22일
This would only propagate badly written code ... so, my answer would be no.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!