How do I delete a row in a table containing certain text?

12 次查看(过去 30 天)
Hi, I am very new to MATLAB and I have been reading all the help and I do not understand where my code is wrong. I need to simply delete a row of my table 'indices' which contains the text 'Totals:'.
Below is an excerpt of my table 'indices' (originally 18x9) and I want to delete the row with the Total:, i.e the last row (or row 18).
CalendarYear Premium
'2017' 35216284
'2018' 36432973
'2019' 37895599
'Total:' 428730614
I tired several things in order to find the row number or the location where the word Total is without success:
1.
>> isequal(indices.CalendarYear,'Total:')
ans =
logical
0
2.
>> strcmp(indices(:,1),'Total:')
ans =
logical
0
3. This gives me a logical output but then I do not know how to continue using it
>> strfind(indices.CalendarYear,'Total:')
ans =
18×1 cell array
{0×0 double}
.........
{[ 1]} %finally a positive answer
none of these attempts gives me the row number 18 which I wold like to delete form my table.
What am I missing?
Thank you in advance,
ni7

采纳的回答

Jan
Jan 2019-1-22
编辑:Jan 2021-1-27
You are almost there.
isequal(indices.CalendarYear, 'Total:')
This tests, if indices.CalenderYear is equal to the char vector 'Total:'. This cannot be equal.
strcmp(indices(:,1), 'Total:')
This is almost working. Use braces instead
strcmp(indices{:,1}, 'Total:')
% Or to get the index:
find(strcmp(indices{:,1}, 'Total:'))
Or equivalently (I prefer this!):
find(strcmp(indices.CalendarYear, 'Total:'))
The command:
strfind(indices.CalendarYear, 'Total:')
searchs, where the part 'Total:' appears, not if the string equals this exactly. But as long as you do not have a 'not Total:' anywhere, this would work also:
find(~cellfun('isempty', strfind(indices.CalendarYear, 'Total:'))) % [TYPO FIXED]
[EDITED] Use contains in modern Matlab versions:
find(contains(indices.CalendarYear, 'Total:'))
  2 个评论
Nina Nikolova
Nina Nikolova 2019-1-22
编辑:Nina Nikolova 2019-1-22
Thank you, @Jan, for the educational answer. I still need to understand better the concept of the different brackets and braces. The solution worked perfect!
Stefan Grandl
Stefan Grandl 2021-1-26
Perfect answer, thanks!
Just a remark: in Jan's last answer there is a final parenthesis missing at the end. It should be:
find(~cellfun('isempty', strfind(indices.CalendarYear, 'Total:')))

请先登录,再进行评论。

更多回答(2 个)

Luna
Luna 2019-1-22
Hi Nina,
You can use this code below:
myTable = table({'2017','2018','2019','Total:'}',[35216284,36432973,37895599,428730614]','VariableNames',{'CalendarYear','Premium'});
reducedTable = myTable(~contains(myTable.CalendarYear,'Total:'),:);
  5 个评论
Nina Nikolova
Nina Nikolova 2019-1-22
编辑:Nina Nikolova 2019-1-22
Dear @Luna, thank you for showing another efficient way, this is all so beneficial for my learning!

请先登录,再进行评论。


madhan ravi
madhan ravi 2019-1-22
Another possibility using regexp but not as effective as the above two answers:
k=cellfun(@(x)regexp(x,'Total:'),T.CalendarYear,'un',0) % T is your table
T(~cellfun(@any,k),:)

类别

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