Delete Table Rows based of certain characters in a row

16 次查看(过去 30 天)
i imported data from a .txt file into a table that is approximately of size (96,000x26), im looking to delete rows based off of a string of characters contained in the first column of that data, then export the updated table to a .csv file
  1 个评论
the cyclist
the cyclist 2023-10-17
Can you upload the data (or a small, representative sample)? You can use the paper clip icon in the INSERT section of the toolbar. It will then be much easier to make sure our code works for your example. (For example, the syntax will depend on whether the first column is a string, or a character array.)

请先登录,再进行评论。

采纳的回答

Jon
Jon 2023-10-17
I think this is a simple example of what you want to do
name = {'cat','dog','fish','bear'}'
name = 4×1 cell array
{'cat' } {'dog' } {'fish'} {'bear'}
weight = [3,25,8,300]'
weight = 4×1
3 25 8 300
T = table(name,weight)
T = 4×2 table
name weight ________ ______ {'cat' } 3 {'dog' } 25 {'fish'} 8 {'bear'} 300
% remove rows with names that contain an a
idl = contains(T.name,'a');
T = T(~idl,:)
T = 2×2 table
name weight ________ ______ {'dog' } 25 {'fish'} 8
% Write to .csv'
writetable(T,'myfile.csv')
  3 个评论
Jon
Jon 2023-10-17
So if I only wanted to keep rows where the name was "fish", I could use
name = {'cat','dog','fish','bear','fish'}'
name = 5×1 cell array
{'cat' } {'dog' } {'fish'} {'bear'} {'fish'}
weight = [3,25,8,300,19]'
weight = 5×1
3 25 8 300 19
T = table(name,weight)
T = 5×2 table
name weight ________ ______ {'cat' } 3 {'dog' } 25 {'fish'} 8 {'bear'} 300 {'fish'} 19
% keep only rows whose name is 'fish'
idl = strcmp(T.name,'fish');
T = T(idl,:)
T = 2×2 table
name weight ________ ______ {'fish'} 8 {'fish'} 19
% Write to .csv'
writetable(T,'myfile.csv')
Jon
Jon 2023-10-17
Or maybe this is what you are asking (if not please provide an example). Let's keep all the rows whose names include goose
name = {'cat','dog','goose','fish','mongoose','bear'}'
name = 6×1 cell array
{'cat' } {'dog' } {'goose' } {'fish' } {'mongoose'} {'bear' }
weight = [3,25,4,8,16,300]'
weight = 6×1
3 25 4 8 16 300
T = table(name,weight)
T = 6×2 table
name weight ____________ ______ {'cat' } 3 {'dog' } 25 {'goose' } 4 {'fish' } 8 {'mongoose'} 16 {'bear' } 300
% Keep rows that contain 'goose'
idl = contains(T.name,'goose');
T = T(idl,:)
T = 2×2 table
name weight ____________ ______ {'goose' } 4 {'mongoose'} 16

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by