dpddt
dpddt

Reputation: 1

matlab: separating datasets in one file

i have a data file that contains an arbitrary number of independent data sets; the data is in three columns, contains an arbitrary number of rows, and will be processed in MATLAB. the first column contains time values and the other two contain corresponding data. the three sets are concatenated, and each does not necessarily contain the same number of rows, nor do the time values begin or end at the same times. as an example, consider the following matrix with three data sets, as determined by the time ranges in the leftmost column:

0.010    xxx    xxx
0.012    xxx    xxx
0.014    xxx    xxx
0.008    xxx    xxx
0.011    xxx    xxx
0.013    xxx    xxx
0.014    xxx    xxx
0.016    xxx    xxx
0.009    xxx    xxx
0.010    xxx    xxx
0.012    xxx    xxx
0.015    xxx    xxx

where xxx are data values that are unimportant to this exercise, yet they must remain associated with the corresponding time value in the leftmost column. what is the easiest / most efficient way to separate each data set from the others in MATLAB? that is, i want to end up with each set in a separate variable:

var1
0.010    xxx    xxx
0.012    xxx    xxx
0.014    xxx    xxx

var2
0.008    xxx    xxx
0.011    xxx    xxx
0.013    xxx    xxx
0.014    xxx    xxx
0.016    xxx    xxx

var3
0.009    xxx    xxx
0.010    xxx    xxx
0.012    xxx    xxx
0.015    xxx    xxx

Upvotes: 0

Views: 659

Answers (2)

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38032

Here's my go at it:

%# read the data
[a,b,c] = textread('data.txt', '%f%s%s');

%# find proper indices to the groups of data
inds = [
    1                   %# include first entry
    find(diff(a)<0)+1   %# add one because diff shrinks the array by 1
    length(a)];         %# include the last entry

%# simply loop over the indices, and assign each range thus defined
%# to an entry in a cell array
A = cell(numel(inds)-1,1);
B = A;
C = A;
for ii = 1:numel(inds)-1
    range = inds(ii) : inds(ii+1)-1;
    A{ii} = a(range);
    B{ii} = b(range);
    C{ii} = c(range);
end

Upvotes: 0

Peter
Peter

Reputation: 14937

First, you don't actually want separate variables. You want cell arrays, as in var{1}, var{2}, var{3}, or indexed by a variable, var{i}.

Second, it looks to me like the criteria for separating data sets is when the step in time from one row to the next goes negative. Is that accurate? If so, then diff will help you out. To find the edge values, use something like this:

edges = find(diff(x(:,1)) < 0);

Now, add a for loop around the edges, and extract the appropriate ranges directly into your cell arrays. Note there are only two "edges" in this example... your first set starts at one and runs to first edge. Your second set starts at edge+1 and runs to edge2, and your final set starts at edge2+1 and runs to the end of the array.

starts = [1; edges];
stops  = [edges + 1; length(x)];

I leave the rest of the code for you...

Upvotes: 2

Related Questions