MATLAB Answers

How can i store data in a cell array with single row and few number of columns?

1 view (last 30 days)
Nedz
Nedz on 3 May 2020
Commented: Nedz on 3 May 2020
Assuming i want to store calculations from a structure array using for loop of size 20 which is equvalent to the number of fields in my structure array.
How can i represent my calculation in cell array ACC{} of a single row and 20 columns?
for a=1:20
ACC{a,1}=Pcount{a,1}.A/length(Pseq)
ACC{a,1}=Pcount{a,1}.R/length(Pseq)
ACC{a,1}=Pcount{a,1}.N/length(Pseq)
ACC{a,1}=Pcount{a,1}.D/length(Pseq)
ACC{a,1}=Pcount{a,1}.C/length(Pseq)
ACC{a,1}=Pcount{a,1}.Q/length(Pseq)
ACC{a,1}=Pcount{a,1}.E/length(Pseq)
ACC{a,1}=Pcount{a,1}.G/length(Pseq)
ACC{a,1}=Pcount{a,1}.H/length(Pseq)
ACC{a,1}=Pcount{a,1}.I/length(Pseq)
ACC{a,1}=Pcount{a,1}.L/length(Pseq)
ACC{a,1}=Pcount{a,1}.K/length(Pseq)
ACC{a,1}=Pcount{a,1}.M/length(Pseq)
ACC{a,1}=Pcount{a,1}.F/length(Pseq)
ACC{a,1}=Pcount{a,1}.P/length(Pseq)
ACC{a,1}=Pcount{a,1}.S/length(Pseq)
ACC{a,1}=Pcount{a,1}.T/length(Pseq)
ACC{a,1}=Pcount{a,1}.W/length(Pseq)
ACC{a,1}=Pcount{a,1}.Y/length(Pseq)
ACC{a,1}=Pcount{a,1}.V/length(Pseq)
end

  0 Comments

Sign in to comment.

Answers (1)

per isakson
per isakson on 3 May 2020
Pre-allocate ACC before the for-loop
ACC = cell( 1, 20 );
replace all ACC{a,1} by ACC{a}

  7 Comments

Show 4 older comments
Nedz
Nedz on 3 May 2020
ACC = cell( 1, 20 )
for t=2
%allAA = sort('ARNDCQEGHILKMFPSTWYV');
Pseq = Pos_Sample{t,1};
Nseq = Neg_Sample{t,1};
Pcount{t,1} = aacount(Pseq);
Ncount{t,1} = aacount(Nseq);
for a=1:20
ACC{a}=Pcount{t,1}.A/length(Pseq);
ACC{a}=Pcount{t,1}.R/length(Pseq);
ACC{a}=Pcount{t,1}.N/length(Pseq);
ACC{a}=Pcount{t,1}.D/length(Pseq);
ACC{a}=Pcount{t,1}.C/length(Pseq);
ACC{a}=Pcount{t,1}.Q/length(Pseq);
ACC{a}=Pcount{t,1}.E/length(Pseq);
ACC{a}=Pcount{t,1}.G/length(Pseq);
ACC{a}=Pcount{t,1}.H/length(Pseq);
ACC{a}=Pcount{t,1}.I/length(Pseq);
ACC{a}=Pcount{t,1}.L/length(Pseq);
ACC{a}=Pcount{t,1}.K/length(Pseq);
ACC{a}=Pcount{t,1}.M/length(Pseq);
ACC{a}=Pcount{t,1}.F/length(Pseq);
ACC{a}=Pcount{t,1}.P/length(Pseq);
ACC{a}=Pcount{t,1}.S/length(Pseq);
ACC{a}=Pcount{t,1}.T/length(Pseq);
ACC{a}=Pcount{t,1}.W/length(Pseq);
ACC{a}=Pcount{t,1}.Y/length(Pseq);
ACC{a}=Pcount{t,1}.V/length(Pseq);
end
end
per isakson
per isakson on 3 May 2020
We posted comments simultaneously.
In your for-loop, which I proposed, the twenty statements overwrite ACC{a}. It's only the value of the last statement that persists.
It's a bit difficult to understand snippets of code with a lot of cell arrays, since the cells may contain anything.

Sign in to comment.