필터 지우기
필터 지우기

Convert Cell Array To Matrix

조회 수: 3 (최근 30일)
Okan
Okan 2011년 11월 1일
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

답변 (2개)

Razvan
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...

Walter Roberson
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
Okan
Okan 2011년 11월 1일
I understand that using d(i)=[] in for loop leads to erase the elements the code reads. But the only thing I wanna do is that converting d{1}, d{2}, d{3} to d1, d2 and d3 matrices. Do you think that this should be done out of for loop? And how can i do this?
Walter Roberson
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by