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

조회 수: 3 (최근 30일)
Oliver Kumar
Oliver Kumar 2016년 3월 25일
댓글: Oliver Kumar 2016년 3월 26일
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
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2016년 3월 25일
You can make your question clear with a small example.
Oliver Kumar
Oliver Kumar 2016년 3월 26일
hi azzi
just solved the problem. there was a misstake in one line of code...
kind regards

댓글을 달려면 로그인하십시오.

답변 (1개)

Joseph Cheng
Joseph Cheng 2016년 3월 25일
편집: Joseph Cheng 2016년 3월 25일
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?

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by