Double for loop is not working properly

2 ビュー (過去 30 日間)
Sebastian Engelsgaard
Sebastian Engelsgaard 2021 年 1 月 27 日
編集済み: Sebastian Engelsgaard 2021 年 1 月 28 日
Hi,
I have a 1x12 cell called 'ch' which contains a repeatable pattern as shown in the code below. I assume that the first row of cells in 'ch' repeats twice, however, the 7th column in ch is not using data{1,2}...
Anyone can help?
for n=1:6:12
for k=1:2
ch{1,n}= data{1,k}(:,14);
ch{1,n+1}= 1500
ch{1,n+2}= 'id'
ch{1,n+3}='name'
ch{1,n+4}='tag'
ch{1,n+5}='unit'
end
end
  1 件のコメント
Bob Thompson
Bob Thompson 2021 年 1 月 27 日
What you have posted worked fine for me. The ch{1,7} did capture data{1,2}.

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

採用された回答

Daniel Pollard
Daniel Pollard 2021 年 1 月 27 日
k will take the values [1 2]. For the first iteration of k, it stores data{1,1}(:,14) in ch{1,n}. The second time around, it will store data{1,2}(:,14) in ch{1,n}, overwriting what was previously done. The data associated with k=1 is never stored.
  4 件のコメント
Bob Thompson
Bob Thompson 2021 年 1 月 27 日
I think you can actually just combine it down to one loop
for k=1:2
ch{1,(k-1)*6+1}= data{1,k}(:,14);
ch{1,(k-1)*6+2}= 1500
ch{1,(k-1)*6+3}= 'id'
ch{1,(k-1)*6+4}='name'
ch{1,(k-1)*6+5}='tag'
ch{1,k*6}='unit'
end
Sebastian Engelsgaard
Sebastian Engelsgaard 2021 年 1 月 28 日
編集済み: Sebastian Engelsgaard 2021 年 1 月 28 日
Thx. It works.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by