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

22 ビュー (過去 30 日間)
Mukhtar Ahmed
Mukhtar Ahmed 2019 年 10 月 7 日
回答済み: Steven Lord 2019 年 10 月 7 日
How can I extract the columns in the dataset that meet certain criteria and move those columns to a new table for further processing?

回答 (3 件)

Adam Danz
Adam Danz 2019 年 10 月 7 日
編集済み: Adam Danz 2019 年 10 月 7 日
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 2019 年 10 月 7 日
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 2019 年 10 月 7 日
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.

カテゴリ

Help Center および File ExchangeTables についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by