Azzera filtri
Azzera filtri

Convert Cell Array To Matrix

3 visualizzazioni (ultimi 30 giorni)
Okan
Okan il 1 Nov 2011
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

Risposte (2)

Razvan
Razvan il 1 Nov 2011
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 il 1 Nov 2011
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 Commenti
Okan
Okan il 1 Nov 2011
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 il 1 Nov 2011
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

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by