Removal of empty cells in an array

조회 수: 1 (최근 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 CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by