Create new column in table from data across multiple columns

조회 수: 11 (최근 30일)
DavidL88
DavidL88 2021년 11월 4일
댓글: DavidL88 2021년 11월 4일
I have a table with multiple events (P1, P2, N1, early P3, P3) listed in one column, ROI. There are several adjacent columns with latencies that have the same events in their titles. I would like to create a new column (Latencies) with the latencies in the adjacent columns extracted and matched the events in the ROI column. For example if the event under ROI is P1 then under the new column, Latencies, the data in the same row under P1_Max_Lat would be copied. I attached a sample table.

채택된 답변

Image Analyst
Image Analyst 2021년 11월 4일
Try this:
load('Example_table.mat')
[rows, columns] = size(sampletable)
tLatencies = table(zeros(rows, 1), 'VariableNames', {'Latencies'})
newTable = [sampletable, tLatencies]
for row = 1 : rows
% Get the event name.
event = newTable.ROI{row}
% Based on that, get the name of the column
% we need to look for to get the data.
columnName = sprintf('%s_Max_Lat', event)
% If that column is there, copy the number to the Latencies column.
if ismember(columnName, newTable.Properties.VariableNames)
% Get data from the appropriate column.
data = newTable.(columnName)(row)
% Put data into the latencies column
newTable.Latencies(row) = data;
else
fprintf('There is no column called %s in the table.\n', columnName);
end
end
  댓글 수: 3
Image Analyst
Image Analyst 2021년 11월 4일
Sure. I understand better now what you want and below is a more robust version that finds the first column that starts with the event string:
load('Example_table.mat')
[rows, columns] = size(sampletable);
tLatencies = table(zeros(rows, 1), 'VariableNames', {'Latencies'});
newTable = [sampletable, tLatencies];
[rows, columns] = size(newTable); % Update size.
variableNames = newTable.Properties.VariableNames;
for row = 1 : rows
% Get the event name.
event = newTable.ROI{row};
% Based on that, get the name of the column
% we need to look for to get the data.
foundIt = false;
for col = 1 : columns
if startsWith(variableNames{col}, event)
% Indicate that we found a column name that starts with the event name.
foundIt = true;
columnName = variableNames{col};
break;
end
end
if ~foundIt
% We did not find any column starting with the event name.
fprintf('There is no column starting with %s in the table.\n', event);
continue; % Skip the rest of the loop for this iteration only.
end
% If that column is there, copy the number to the Latencies column.
if ismember(columnName, newTable.Properties.VariableNames)
% Get data from the appropriate column.
data = newTable.(columnName)(row);
% Put data into the latencies column
newTable.Latencies(row) = data;
end
end
% Report the new table to the command window:
newTable
If this works, please "Accept this Answer". Thanks in advance. If not, let me know what row is bad.
DavidL88
DavidL88 2021년 11월 4일
Worked perfectly. Thank you.

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

추가 답변 (0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by