Structure array data extraction and processing

2 views (last 30 days)
Josh
Josh on 20 Jun 2022
Commented: Josh on 20 Jun 2022
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

Answers (1)

Chris Burschyk
Chris Burschyk on 20 Jun 2022
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 Comment
Josh
Josh on 20 Jun 2022
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?

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!