Removal of empty cells in an array

2 ビュー (過去 30 日間)
Josh
Josh 2022 年 6 月 22 日
コメント済み: Josh 2022 年 6 月 22 日
The variable: cumulat(1,2) is returning an empty third row. How does one not get that empty row? 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,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

採用された回答

Karim
Karim 2022 年 6 月 22 日
this was due to the indexing, you were putting the temporary data into "nn{n1,n} = F1{n1}", thus at the second run of "n" a new column would be added to "nn" and it would result in the same number of rows as "n=1".
The solution is to allocate a new cell at each loop.
S = load('CCSD.mat');
F = fieldnames(S);
for n = 1:numel(F)
F1 = fieldnames(S.(F{n}));
F11{n} = F1;
% allocate the variables
nn = cell(numel(F1),1);
PArea = cell(numel(F1),1);
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
cumulat
cumulat = 1×2 cell array
{3×2 cell} {2×2 cell}
cumulat{1,2}
ans = 2×2 cell array
{'c1'} {[5.5000]} {'c2'} {[5.5000]}
  1 件のコメント
Josh
Josh 2022 年 6 月 22 日
Your reasoning is correct. Thanks for the help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStateflow についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by