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?
  1 件のコメント
Oliver Kumar
Oliver Kumar 2016 年 3 月 26 日
hi joseph
yes you are right! thanks! I have to adapt my code.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by