Structure array data extraction and processing

조회 수: 1 (최근 30일)
Josh
Josh 2022년 6월 20일
댓글: Josh 2022년 6월 20일
CCSD structure array has two fields: 'xc1' & 'xc2'. 'xc1' has three fields-'c1','c2', & 'c3' whereas xc2 has two fields-'c1', and 'c2'.
As I run the structure inside 'for loop' to extract & process the data,'xc1' is also gets processed for three fields as well as 'xc2'.
It is confusing why 'xc2' is getting processed for three fields whereas it has only two fields(ref:- Variable: cumulat(1,2)). Please help.
clear
clc
close all
S = load('CCSD.mat')
F = fieldnames(S);
for n = 1:numel(F)
F1 = fieldnames(S.(F{n}));
F11{n} = F1;
for n1 = 1:numel(F1)
t = S.(F{n}).(F1{n1}).cc.t;
nn{n1} = [F1{n1}];
output{n1} = cumtrapz (t);
NF = @(p,q) max(output{n1}(t<=q)) - min(output{n1}(t>=p));
PArea{n1} = NF(5, 6)
end
cumulat{n} = [nn', PArea'];
end

답변 (1개)

Chris Burschyk
Chris Burschyk 2022년 6월 20일
In the first run of your n-loop it assignes nn 3 values. In the next run it assigns only 2 values but the 3rd value doesn't get overwritten or deleted and stays the same. You either have to clear nn or give it another index like
nn{n1,n} = F1{n1};
Maybe this code works for you:
clear
clc
close all
S = load('CCSD.mat');
F = fieldnames(S);
for n = 1:numel(F)
F1 = fieldnames(S.(F{n}));
F11{n} = F1;
for n1 = 1:numel(F1)
t = S.(F{n}).(F1{n1}).cc.t;
nn{n1,n} = F1{n1};
output{n1} = cumtrapz (t);
NF = @(p,q) max(output{n1}(t<=q)) - min(output{n1}(t>=p));
PArea{n1,n} = NF(5, 6);
end
cumulat{n} = [nn(:,n), PArea(:,n)];
end
  댓글 수: 1
Josh
Josh 2022년 6월 20일
Your reasoning seem to be right about the issue. Now it works better, however, variable: cumulat(1,2) is still returning an empty third row. Is it possible not getting that empty third row?

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by