overwrite cell array in a loop

조회 수: 3 (최근 30일)
summyia qamar
summyia qamar 2018년 7월 31일
댓글: summyia qamar 2018년 7월 31일
the code is given below; parts(day) are calculated using some data which is let say comes out as 3 4 2 for day 1 2 3. complete code is attached in file.. when the value of parts(day) decreses from previous loop, the cell cannot reassign the new value but overwrite on previous and causes extra cell which creates error in reshaping.
for day=1:3
n = 2;
Psub{parts(day}=[];
for ii=1:(parts(day)*2)/n
Psub{ii}=process_plan((ii-1)*n+1:ii*n,:);
end
P=reshape((cell2mat(Psub)),parts(day)*2,machines);
  댓글 수: 2
Bob Thompson
Bob Thompson 2018년 7월 31일
편집: Bob Thompson 2018년 7월 31일
I'm a little confused by what you're problem is and would like some more context. Would you mind including more of your code (the whole loop if possible)?
summyia qamar
summyia qamar 2018년 7월 31일
I have provided the complete code..the error is from line 52 to 57

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

채택된 답변

Stephen23
Stephen23 2018년 7월 31일
편집: Stephen23 2018년 7월 31일
clear the cell array before you implicitly create it with that allocation:
clear P
P{4}=[];
...
clear P
P{2}=[]
...
Note that this is not an efficient solution. A much better solution would be to explicitly create the cell array of the right size each time, e.g.:
P = cell(1,4);
...
P = cell(1,2);
...
I recommend that you explicitly create the cell array.
  댓글 수: 1
summyia qamar
summyia qamar 2018년 7월 31일
thankyou so much, I was looking for this solution but I was confused how to define cell array. the second solution is best for me.

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

추가 답변 (0개)

카테고리

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