How to filter a table in matlab according to a condition on the elements of one or more columns?

1.268 views (last 30 days)
I read a table from a .csv file, it contains 9 columns (the first two contain strings and the others numbers in double precision) and many rows (28056 to be precise) using matlab function readtable and I stored that table in a variable of type table.
Now I would like to filter the data of all columns by imposing, for example, that the values in the column named "a" are between 0.9 and 1.1 and, at the same time, the values in the column named "e" are less than or equal to 0.3. Then I would like to obtain a new smaller table according to the above conditions containing filtered data of all columns .
How can I do this?
I make an example to explain better. Let us assume that I have the first 10 rows:
They are asteroids data, so let us assume that I want to filter them by imposing a <=2, so I would like to obtain a new table where I have all columns but only the rows that satisfy my condition, i.e. a table with rows 1, 5, 6, 8, 9, 10.
Here my code:
clear all; close all; clc;
file_name_asteroids = 'NEOs_asteroids.csv';
opts = delimitedTextImportOptions("NumVariables", 9);
% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = ",";
% Specify column names and types
opts.VariableNames = ["pdes", "name", "epoch", "a", "e", "i", "om", "w", "ma"];
opts.VariableTypes = ["string", "string", "double", "double", "double",...
"double", "double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, ["pdes", "name"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["pdes", "name"], "EmptyFieldRule", "auto");
% Import the data
Asteroids_data = readtable(file_name_asteroids,opts);

Accepted Answer

Cris LaPierre
Cris LaPierre on 30 Jan 2022
idx = Asteroids_data.a >= 0.9 & Asteroids_data.a <= 1.1 & Asteroids_data.e <= 0.3;
newTbl = Asteroids_data(idx,:);

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by