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_path = 'D:\OneDrive\MSc_Thesis\Projects\NEOs_orbits\InputFiles\';
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_path,file_name_asteroids],opts);
To see the input file NEOs_asteroids.csv
you can look at this link, where I have made another question: NEOs_asteroids.csv
You want
new_table=Asteroids_data(Asteroids_data.a>0.9 & Asteroids_data.a<1.1 & Asteroids_data.e<0.3,:)
which happens to be empty for the toy data. So just for the illustration of the method,
new_table=Asteroids_data(Asteroids_data.a>2 & Asteroids_data.e<0.5,:)
new_table =
2×9 table
pdes name epoch a e i om w ma
______ _________ __________ ______ _______ ______ ______ ______ ______
"1580" "Betulia" 2.4596e+06 2.1973 0.48716 52.079 62.292 159.51 16.629
"1916" "Boreas" 2.4596e+06 2.2723 0.44984 12.883 340.6 335.92 352.24