Separating subtables from a larger table by using the variable values in a column
25 views (last 30 days)
I got a data folder with 19023 txt files. Each of the txt files have the data of 71 electrodes (indicated by the first column x_) like this:
As a preview, it shows only the first 7 rows.
I have created a datastore and selected the variables of interest using the codes below:
ds = datastore("data")
vars = ds.VariableNames;
idx = ismember(vars,["x_", "P1_xm_","P2_xm_","Rho","I","U","D","Time"]);
ds.SelectedVariableNames = vars(idx)
data = readall(ds)
I want to create 71 time series tables for each of electrodes, so I used the code:
e1 = data(data.x_==0,:)
And obtained the resulted table for the Electrode No.0.
However, I need to further process and analyse the individual table later on, it will be very time consuming to do repeat the same codes for 71 times every time. Is there a simpler method such as using a loop to generate 71 time series tables for individual electrodes?
Thank you in advance.
Edric Ellis on 24 May 2022
Rather than looping, you might be able to use findgroups and splitapply to do what you need. Imagine you wish to compute the maximum value of Rho for each value of x. Here's how to do that:
x = repmat([0:5]', 10, 1);
Rho = x + 10 + rand(size(x));
Time = datetime(2022, 05, 24 + (1:numel(x))', 09, 39+x, x);
% Example table
t = table(x, Rho, Time)
% Use findgroups to group values of "x"
[g, xVal] = findgroups(t.x); % 'g' is a group index, 'xVal' is the corresponding value of x.
% Now apply the function MAX to values of Rho for each value of x
maxRho = splitapply(@max, t.Rho, g);
% Display the results
There's a more information in this example page.