Creating a for loop that Concatinates the outputted matrixes

조회 수: 2 (최근 30일)
James
James 2014년 4월 9일
편집: Joseph Cheng 2014년 4월 9일
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