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!