Creating a new data table from existing table based on condition.
조회 수: 28 (최근 30일)
이전 댓글 표시
How can I extract the columns in the dataset that meet certain criteria and move those columns to a new table for further processing?
댓글 수: 0
답변 (3개)
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.
댓글 수: 0
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,:)
댓글 수: 0
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!