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.

Accepted Answer

Edric Ellis
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)
t = 60×3 table
x Rho Time _ ______ ____________________ 0 10.543 25-May-2022 09:39:00 1 11.511 26-May-2022 09:40:01 2 12.862 27-May-2022 09:41:02 3 13.214 28-May-2022 09:42:03 4 14.33 29-May-2022 09:43:04 5 15.959 30-May-2022 09:44:05 0 10.209 31-May-2022 09:39:00 1 11.244 01-Jun-2022 09:40:01 2 12.537 02-Jun-2022 09:41:02 3 13.439 03-Jun-2022 09:42:03 4 14.664 04-Jun-2022 09:43:04 5 15.956 05-Jun-2022 09:44:05 0 10.757 06-Jun-2022 09:39:00 1 11.826 07-Jun-2022 09:40:01 2 12.473 08-Jun-2022 09:41:02 3 13.659 09-Jun-2022 09:42:03
% 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
disp([xVal, maxRho])
0 10.8994 1.0000 11.9073 2.0000 12.9336 3.0000 13.8890 4.0000 14.8112 5.0000 15.9587
There's a more information in this example page.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by