Parsing an array A based on trigger data in array B ?

조회 수: 2 (최근 30일)
Katie
Katie 2013년 7월 3일
Hi everyone!
I have what should be a simply question but I cannot seem to find an effective solution. I have two columns of data in Excel files that I read into Matlab, at the moment as two separate arrays (column A and B).
Column A contains numeric data and Column B is a trigger stamp for the data in A. Example:
What I need to do is to parse A and split it into a number of (30) arrays depending on the trigger point in B i.e. the very large A would become A1, A2, A3 ... A30, split according to when there is a data-stamp in B.
  댓글 수: 2
Jan
Jan 2013년 7월 3일
Please post how you "read the Excel file". Then creating a matching answer is easier.
Katie
Katie 2013년 7월 3일
편집: Katie 2013년 7월 3일
Hi Jan,
I was just crudely reading from the excel file and writing the columns to vectors, but I have switched to reading a cdv instead as Chad suggested, much neater. My trouble is how to split the matrix based on the second column (or B in my earlier version) that is causing the difficulty. Thanks.
[OLD version, reading from Excel] A=xlsread('file.xls','A:B'); rows=sizem(1); % Assigning the columns to vectors for i=1:1:rows if(~isnan(A(i,1))) signal(i)=A(i,1); end end etc.

댓글을 달려면 로그인하십시오.

채택된 답변

Chad Gilbert
Chad Gilbert 2013년 7월 3일
If I save similar data out as a .csv file:
A,B
1.303,1
1.61,
4,2
1.213,
and then read them in with:
>> a = csvread('a.csv',1,0)
I get a matrix the same shape as your data, with 0's filling all the empty spots:
a =
1.3030 1.0000
1.6100 0
4.0000 2.0000
1.2130 0
This will be pretty easy to parse into a cell array of arrays.
  댓글 수: 3
Chad Gilbert
Chad Gilbert 2013년 7월 3일
편집: Chad Gilbert 2013년 7월 3일
Ah, I see. I thought your trouble was with reaading. To split it up, Rob's answer ought to work. Or a shorter version might be:
ind = [find(a(:,2)>0); size(a,1)+1];
b = arrayfun(@(i)a(ind(i):ind(i+1)-1),1:length(ind)-1,'uniformoutput',false)
Katie
Katie 2013년 7월 3일
Hi Chad, and Rob!
I tried Chad's first as I had already switched to using a matrix 'a'. This works brilliantly, thank you both. The only thing that is wrong at the moment with it is that it populates b with 29 vectors, not 30. It misses the first one every time. I might just be missing something simple.
I can see in the workspace that 'b' is a 1 x 29 matrix now. If I want to write this out to a new file I'm guessing I have to do more with it?
Thank you both again, much appreciated!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Rob
Rob 2013년 7월 3일
편집: Rob 2013년 7월 3일
I'm sure there are more elegant solutions, but here's one way:
idx1 = find(~isnan(B));
idx2 = [idx1(2:end)-1; length(A)];
C = cell(length(idx1), 1);
for k = 1:length(idx1)
C{k} = A(idx1(k):idx2(k));
end
If the non trigger values in B are zeros then:
idx1 = find(B>0);
-Rob

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by