Removal of empty cells in an array

2 visualizaciones (últimos 30 días)
Josh
Josh el 22 de Jun. de 2022
Comentada: Josh el 22 de Jun. de 2022
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

Respuesta aceptada

Karim
Karim el 22 de Jun. de 2022
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 comentario
Josh
Josh el 22 de Jun. de 2022
Your reasoning is correct. Thanks for the help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Stateflow en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by