# Store Variables from for loop to cell array. it only stores the last run

6 views (last 30 days)
Oliver Kumar on 25 Mar 2016
Commented: Oliver Kumar on 26 Mar 2016
hello I want to store the variables from a 10x75 matrix, to a cell array. but it should not start at (1,1) in the cell array, it has to start at cell (5,2). If I run the code, it'll only store the variables from how_many_NaNs(:,75), to result(:,79). All the other cells are filled with []. Does anyone has an idea how to fix that problem? Thanks for you advice.
cnter = 2
for j = 1:size(marker,1)
result(1,4+j) = {marker(j,:)};
end
%store variables
for n = 1:size(how_many_NaNs,1)
result(cnter,4+j) = {how_many_NaNs(n,j)};
cnter = cnter +1;
end
end
##### 2 CommentsShowHide 1 older comment
Oliver Kumar on 26 Mar 2016
hi azzi
just solved the problem. there was a misstake in one line of code...
kind regards

Joseph Cheng on 25 Mar 2016
Edited: Joseph Cheng on 25 Mar 2016
it might be obvious but you might want to nest your for loops.
cnter = 2
for j = 1:size(marker,1)
result(1,4+j) = {marker(j,:)};
end
%store variables
for n = 1:size(how_many_NaNs,1)
result(cnter,4+j) = {how_many_NaNs(n,j)};
cnter = cnter +1;
end
end
here is your original code and it says you'll be running j from 1 to size of marker. then at the end of the j for loop: j = size(marker,1).
then you go into your n for loop with j=size(marker,1). without understanding the data itself to see how_many_NaNs is made up of, wouldn't you want to go through all j values for each n?
Oliver Kumar on 26 Mar 2016
hi joseph
yes you are right! thanks! I have to adapt my code.