Creating a new data table from existing table based on condition.

78 vues (au cours des 30 derniers jours)
Mukhtar Ahmed
Mukhtar Ahmed le 7 Oct 2019
How can I extract the columns in the dataset that meet certain criteria and move those columns to a new table for further processing?

Réponses (3)

Adam Danz
Adam Danz le 7 Oct 2019
Modifié(e) : Adam Danz le 7 Oct 2019
By 'dataset', do you mean a matlab dataset array or are you using the term more generally, working with tables instead? The demo below shows how to extract data from a dataset array and store it in a table. If your extracting data from a table instead, the same approach is used.
% Load matlab dataset demo
patients = dataset('File','hospital.dat',...
'Delimiter',',','ReadObsNames',true);
% Example: move dataset columns to a table, by column name
% Here we move the name, age, and wgt columns to a table.
patientsTable = dataset2table(patients(:,{'name','age','wgt'}));
% Example: move dataset columns to a table but only certain rows.
% Here we move the name, sex, and age columns to a table and isolate
% rows of females ages 30-40.
idx = strcmpi(patients.sex,'f') & patients.age>=30 & patients.age<=40;
patientsTable = dataset2table(patients(idx,{'name','sex','age'}));
Note
Unless you're trying to drastically reduce the size of a variable, it's recommended to keep data organized in tables rather than splitting it apart. If you need to access certain rows and columns, use indexing.

John Doe
John Doe le 7 Oct 2019
If you have table t.
You can create a new table (nt) as such:
% x = condition 1
% y = condition 2
nt = t(t.var1 > x & t.var1 < y,:)

Steven Lord
Steven Lord le 7 Oct 2019
See the examples on this documentation page, particularly the "Select Rows with Logical Indexing" section.
Note that this is the documentation for the most recent release, so if you're using an older release certain information (having variable names that are not valid MATLAB identifiers, for example, support for which was added in release R2019b) may not be applicable to the release you're using. So you may want to navigate to the version of that documentation page in the documentation included in your MATLAB installation.

Catégories

En savoir plus sur Data Import from MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by