Creating a for loop that Concatinates the outputted matrixes

2 次查看(过去 30 天)
Hi,
I have a file that is a master file, it just contains other file names. I am trying to get matlab to loop through this file and get the data that I need from each of the files listed in the "master file". Here is my code so far,
[num txt raw]=xlsread('vehicle master file');
for(x=1:length(txt))
x;
filename=txt{x};
[num1 txt1 raw1 = xlsread(filename);
txtrow7=txt1(32:end,7);
index1 = find(ismember(txtrow7,'response delay','legacy'));
num5=num1(32:end,5);
DR=num1(index1,5);
%remove NaN
DR1=Snip(DR,nan);
%find and remove 10's
values10=find(DR1==10);
DR1(values10)=[];
end
contained in the for loop is all the code that I want to do. The "DR1" is a 300x1 matrix. I do not know how do get this script to loop back through and create another final matrix and concatinate them together so after the second file I will have a matrix "DR2" that is 600x1. And so on depending on how many times the loop runs. How would I do this?
Thanks.

回答(1 个)

Joseph Cheng
Joseph Cheng 2014-4-9
编辑:Joseph Cheng 2014-4-9
If the resulting DR1 length for each iteration is variable and you are not concerned about time, you can insert your DR2=[] before the for loop and at end of the for loop you do DR2 = [DR2;DR1]; which will set DR2 as DR2 appended with DR1;
[num txt raw]=xlsread('vehicle master file');
DR2=[]; %creates empty array;
for(x=1:length(txt))
%%stuff done to get DR1
DR1(values10)=[];
DR2=[DR2;DR1]%append DR2 with DR1;
end
Iteration 1: DR2 is empty so DR2 will be DR1.
Iteration 2: DR2 already has the stuff from previous iteration and then you concatenate old stuff with new.
  1 个评论
Joseph Cheng
Joseph Cheng 2014-4-9
编辑:Joseph Cheng 2014-4-9
Preallocating the DR2 can be performed as such.
[num txt raw]=xlsread('vehicle master file');
DR2=zeros(300*length(txt),1);
for(x=1:length(txt))
%%stuff done to get DR1
DR1(values10)=[];
DR2((i-1)*300+1:i*300) = DR1; %if DR1 is always 300 long
end
for different length DR1;
CurrentPos = 1;
DR2=zeros(300*length(txt),1); %pre allocation something representative to your data.
for(x=1:length(txt))
%%stuff done to get DR1
DR1(values10)=[];
DR2(Cpos:Cpos+length(DR1)-1) = DR1; %if DR1 is always 300 long
Cpos = Cpos+length(DR1);
end
DR2(Cpos:end)=[]; %clear out remaining allocated space.
Any of the methods should work.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by