Clear Filters
Clear Filters

Extract Data from Table by Data Values

5 views (last 30 days)
Sam P
Sam P on 7 May 2020
Edited: BhaTTa on 16 Jul 2024 at 3:17
Hi all,
I am trying to extract rows matching one of multiple values but I am not sure which functions to use. I have a 9,857,445 x 3 table. The headers are in the 3 columns, and all 3 columns A,B, C, contain numerical data.
I am trying to extract based on data for the first column A. For example, I want to extract the rows that have the following values for A; A= 1, A=6, A=4 or A =12. Based on all the rows that match any of the possible listed values for A, I would like to create a new table Final Data, that only lists rows that match the given values for A. I am using 2015a.
Thanks for your help.

Answers (1)

BhaTTa
BhaTTa on 16 Jul 2024 at 3:05
Edited: BhaTTa on 16 Jul 2024 at 3:17
You can achieve this by using logical indexing below is the implemetation:
% Example to create a sample table, replace this with your actual data loading
dataTable = array2table(randi(20, 9857445, 3), 'VariableNames', {'A', 'B', 'C'});
valuesToMatch = [1, 6, 4, 12];
% Logical indexing to find rows where column A matches any of the specified values
rowsToExtract = ismember(dataTable.A, valuesToMatch);
% Create the new table with the extracted rows
finalData = dataTable(rowsToExtract, :);
% Example: Save to a new file
writetable(finalData, 'finalData.csv');

Categories

Find more on Tables 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!