How to extract rows from a table?

304 ビュー (過去 30 日間)
Lucas Acuna Scafati
Lucas Acuna Scafati 2020 年 2 月 18 日
I am having trouble extracting certain values from a table and putting them in another table. This is the code i have so far but for the extracted values it creates a 0 x 4 table when it should be a 4x4 table.
for d = Tbl.ECF
if any (Tbl.ECF > 1.06) && any(Tbl.ECF < .94) %Looks for any dot with standard deviation above 1%
ExtractedECF = Tbl(Tbl.ECF > 1.06 & Tbl.ECF < .96,:); % Extracts dot mentioned in previous line and puts it in a new table
Tbl(Tbl.ECF > 1.06,:) = []; % Deletes Row with dot above 1%
Tbl(Tbl.ECF < .94,:) = []; % Deletes Row with dot above 1%
else
end
end
How can should I fix this problem and is there a more effective way to writing this code? Thank you, I am very new to MATLAB
  1 件のコメント
the cyclist
the cyclist 2020 年 2 月 18 日
It would be helpful if you uploaded the table in MAT file, using the paper clip symbol.

サインインしてコメントする。

回答 (1 件)

Jacob Wood
Jacob Wood 2020 年 2 月 18 日
Lucas,
Matlab can be very good at working with tables and indexing like this. One of the most powerful concepts in Matlab is logical indexing which can be used to quickly select subsets of data based on your defined conditions.
An example of doing this with Matlab and tables would be something like:
% Make a table
a = [1 3 4 6]';
b = [11 13 14 16]';
Tbl1 = table(a,b);
idx = Tbl1.a > 2 & Tbl1.a<5; %find what rows this is true
Tbl2 = Tbl1(idx,:); %pick out those rows (and all the columns) from Tbl1 and put them in a new table
disp(Tbl2) %show new table
  1 件のコメント
Lucas Acuna Scafati
Lucas Acuna Scafati 2020 年 2 月 20 日
Thank you for the Response, I ended up using this because indexing both conditions in one line was giving me an error message. This seems to work though:
if any(Tbl.ECF < .96) && any(Tbl.ECF > 1.04)
ExtractedLowECF = Tbl(Tbl.ECF < .96,:); % Extracts any ECF below .94
ExtractedHighECF = Tbl(Tbl.ECF > 1.04,:); % Extracts any ECF above 1.06
TBL2 = [ExtractedLowECF;ExtractedHighECF]; % Creates table of all Extracted ECF adding all rows by concatenation
else
end

サインインしてコメントする。

カテゴリ

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