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

2 views (last 30 days)
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
%create header
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

Answers (1)

Joseph Cheng
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
%create header
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?

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!