Create new columns in tale adding prior data

1 view (last 30 days)
DavidL88
DavidL88 on 16 Mar 2022
Commented: DavidL88 on 7 Apr 2022
I have a table (sample attached) summarising neural activity; either 1 or -1. The rest are blanks. The first two columns relate to the test. The third column onwards is a summary of the activity. There are 68 of these columns. These 68 regions are part of larger regions denoted by first part of text within each column (LF, LC, LT etc). The number of columns per region differs. I want to create a new series of columns that adds the aboslute value of these sub regions so I'll have columns with the value of the overall region ie LF_1... = 1, LF_2... = -1, LF_3... = 1 Resulting column LF will be 3 for that row. How can I do this? Is there a way to automatically recognise the first letters prior to the first "_" to create a new column?

Answers (1)

Cris LaPierre
Cris LaPierre on 16 Mar 2022
Columns in a table are variables. Here, you want to just create a 'new' variable, and set it equal to the sum of the existing variables.
% create a same table
LF_1 = 1;
LF_2 = -1;
LF_3 = 1;
T = table(LF_1,LF_2,LF_3)
T = 1×3 table
LF_1 LF_2 LF_3 ____ ____ ____ 1 -1 1
% Create a new variable LF in the table as sum of LF_1-3
T.LF = abs(T.LF_1) + abs(T.LF_2) + abs(T.LF_3)
T = 1×4 table
LF_1 LF_2 LF_3 LF ____ ____ ____ __ 1 -1 1 3
  3 Comments
DavidL88
DavidL88 on 7 Apr 2022
Thanks. Is there a way to recognise groups of variables/columns based on the letters prior to a "_"? So recognise group LF from LF_1, LF_2, LF_3. Rather than writing a line of code for each group - LF, RF, LPF, etc is there a code that would recognise these groupings?

Sign in to comment.

Categories

Find more on Text Analytics Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!