Convert Cell Array To Matrix
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi,
I am trying to convert the cell array to matrix. However, the code below gives error as "Index exceeds matrix dimensions." How can i solve this problem?
clear all
[name]=textread('assign3_520.txt','%s');
for i=1:3
d{i}=textread(name{i},'%f','headerlines',4);
d{i}=d{i}*9.81;
d{i}=detrend(d{i},'constant');
d{i}=detrend(d{i});
dv{i}=cumtrapz(d{i})*0.01;
dd{i}=cumtrapz(dv{i})*0.01;
d(i)=[];
d(i)=cell2mat(d{i});
end
댓글 수: 0
답변 (2개)
Razvan
2011년 11월 1일
I would convert the cell array d to a matrix after the for loop, e.g. v = cell2mat(d); Also if you use d(i)=[] in the loop that erases some element that you just read...
댓글 수: 0
Walter Roberson
2011년 11월 1일
d(i) = [];
means that d(i) should be deleted. When i=1 this moves d(2) down to d(1) and d(3) down to d(2), with d(3) no longer existing. When i=2 what is now in d(2) (that started as being in d(3)) would be deleted, leaving just d(1) (that started as being in d(2)) and with d(2) and d(3) no longer existing. Then in the next line after that, you try to reference d{2} but d(2) does not exist and hence d{2} does not either.
I cannot advise you as to what you should do with your code as your code is too obscure for me. I don't know why you do not use appropriate temporary variables within the loop and then assign the final d{i} at the end of the loop.
댓글 수: 2
Walter Roberson
2011년 11월 1일
You probably should not do that at all. Please see
http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!