How can I delete data from a variable in a table?

How can I delete data from a variable in a table: For example,
I want to delete all rows that has 'Not Collected' under SampledDate.

4 件のコメント

Rik
Rik 2021 年 7 月 23 日
What have you tried?
dpb
dpb 2021 年 7 月 23 日
NB: You can't delete the rows from only the 'SampledData' variable but all columns in the table...
Waleed Nowayti
Waleed Nowayti 2021 年 7 月 26 日
Sorry for the incomplete information. I meant to say I want to delete the whole rows. Any row contains Not Collected under 'SampledData', I need the code to delete it!
Waleed Nowayti
Waleed Nowayti 2021 年 8 月 3 日
This is what I did Rik, and it is working for me!
W = table2cell(TEST);
IndexW = find(contains(W(:,7), 'Not Collected'));
% I used (:,7) to locate the column instead of using its name!
TEST(IndexW,:)=[];

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

 採用された回答

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 7 月 24 日

0 投票

In fact, it is not viable to delete only rows as dpb stated.
Otherwise, if you'd like to remove all elements of specific rows, that can be done relatively easy, e.g.:
TAB(TAB.SampledDate=='Not Collected', :)=[];

8 件のコメント

Waleed Nowayti
Waleed Nowayti 2021 年 7 月 26 日
I tried this one. It gives me this error: Undefined function 'eq' for input arguments of type 'cell'.
dpb
dpb 2021 年 7 月 26 日
Have to use cellfun on cell array returned by TAB.SampledDate, then.
W/o a dataset attached, we sorta' have to guess...
However, I'd suggest going back to the point at which you create the table by (I presume) reading a file and using the DetectImportOptions object and the 'MissingRule' value to 'omitrow' and 'TreatAsMissing' to 'Not Collected' and won't bring those records in in the first place.
Waleed Nowayti
Waleed Nowayti 2021 年 7 月 29 日
Thank you so much for your help and input!
Waleed Nowayti
Waleed Nowayti 2021 年 8 月 2 日
編集済み: Waleed Nowayti 2021 年 8 月 3 日
I had a problem using ==
Therefore, I changed the way for matlab to find what I am looking for.
W = table2cell(TEST);
IndexW = find(contains(W(:,7), 'Not Collected'));
% I used (:,7) to locate the column instead of using its name!
TEST(IndexW,:)=[];
It worked for me this way!
Thank you everyone for your help!
Stephen23
Stephen23 2021 年 8 月 3 日
replace FIND & CONTAINS with STRCMP or STRCMPI.
Waleed Nowayti
Waleed Nowayti 2021 年 8 月 3 日
I just tried it. It worked the same! Thank you so much for your input and help.
In general, what is the different between these two ways? Why is it giving the same output and will they be different outputs if they are used in a different code?
W = table2cell(TEST);
% IndexW = find(contains(W(:,7), 'Not Collected'));
IndexW = strcmp(W(:,7), 'Not Collected');
TEST(IndexW,:)=[];
Stephen23
Stephen23 2021 年 8 月 3 日
"In general, what is the different between these two ways?"
One checks if the table data contains the required text, the other checks if the table data is the same as that text.
"Why is it giving the same output and will they be different outputs if they are used in a different code?"
Just like any functions, they will give different outputs depending on the input data:
A = 'cat in hat';
B = 'hat';
contains(A,B)
ans = logical
1
strcmp(A,B)
ans = logical
0
Only you (as your code's author) can decide which one suits your needs best.
Waleed Nowayti
Waleed Nowayti 2021 年 8 月 3 日
That is amazing! I see the difference and your example is just straight to the point. Thank you so much for explaining!

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by