Delete rows with characters in cell array
2 次查看(过去 30 天)
显示 更早的评论
I need some basic help. I have a cell array:
- 1 TITLE 13122423
- 2 NAME Bob
- 3 PROVIDER James
and many more rows with text...
- 44 234 456 234 345
- 45 324 346 234 345
- 46 344 454 462 435
and many MANY (>4000) more with only numbers
- 4100 text
- 4101 text
and more text and mixed entries
Now what I want is to delete all the rows where the first column contain a character, and end up with only those rows containing numbers. Row 44 - 46 in this example.
I tried to use
rawdataTruncated(strncmp(rawdataTruncated(:, 1), 'A', 1), :) = [];
but then i need to go throught the whole alphabet, right?
3 个评论
采纳的回答
Andrei Bobrov
2017-8-18
My ruble:
A={'1 TITLE 13122423';
'2 NAME Bob';
'10 PROVIDER James';
'44 234 456 234 345';
'48 344 454 462 435';
'4100 text';
'4101 text';
'4102 2more text';
'4103 0495- 3725'};
x = regexp(A,'^(\d+\s*)+$','match');
out = [x{:}]';
5 个评论
Jan
2017-8-20
编辑:Jan
2017-8-20
@John BG: Obviously the original poster JB is satisfied with this solution already. Then there is no reason to invent further exceptions as leading or multiple spaces, negative or fractional numbers, or even operators.
Steven Lord's comment was useful: Let the OP ask if other details are required and reduce cluttering comments.
更多回答(5 个)
the cyclist
2017-8-17
I feel like there is a way to do this without resorting to cellfun, and just using regexp, but I'm drawing a blank. However, here is one way.
C = {'TITLE 13122423';
'234 456 234 345';
'text'};
cellfun(@(x)not(isempty(x)),regexp(C,'^[0-9][0-9 ]+'))
regexp is checking that each cell starts with a numeric character, and has only spaces and numbers after that.
4 个评论
Stephen23
2017-8-18
编辑:Stephen23
2017-8-19
@John BG: luckily regular expressions are trivial to alter, as long as the requirements are clearly specified. Note that the original question does not show - in its example, and JB states "...end up with only those rows containing numbers", so we would have to ask JB exactly what characters are allowed in the output numbers: I certainly don't know what data JB is working with. Do you?
>> idx = cellfun('isempty',regexp(A,'^[0-9][-0-9 ]+$'));
>> A(~idx)
ans =
'44 234 456 234 345'
'48 344 454 462 435'
'4103 0495- 3725'
Note that using regexp much more efficient (in terms of total coding, debugging, and running time) than trying to write your own string parser, as some beginners would try to do
Guillaume
2017-8-18
The simple addition of a $ at the end of the regular expression is all that is needed to fix the problem.
Guillaume
2017-8-18
As said, ignore attempts to build your own custom string parser, it's inefficient and a complete waste of time. Use a regular expression:
This one works, John BG can attempt to find flaws in it to his heart content:
A = {'1 TITLE 13122423';
'2 NAME Bob';
'10 PROVIDER James';
'44 234 456 234 345';
'48 344 454 462 435';
'4100 text';
'4101 text';
'4102 2more text';
'4103 0495- 3725'}
isjustnumbers = ~cellfun('isempty', regexp(A, '^[0-9][0-9 ]+$'));
filteredA = A(isjustnumbers)
5 个评论
José-Luis
2017-8-18
+1. Simple and efficient.
Tongue in cheek: That moment when Matlab answers starts feeling like a battlefield...
Jan
2017-8-18
编辑:Jan
2017-8-18
If you want to remove all lines, which contain a non-digit and non-white-space:
index = cellfun(@(ac) all(isstrprop(ac, 'digit') | ...
isstrprop(ac, 'wspace')), C);
C = C(index)
A loop is faster than cellfun:
B = true(size(A));
for k = 1:numel(A)
B(k) = ~all(isstrprop(A{k}, 'digit') | ...
isstrprop(A{k}, 'wspace'));
end
A(B) = [];
2 个评论
Alan Peters
2017-8-19
I love your profile picture, Jan. I think I share that expression half the time I'm trying to solve a problem with Matlab!
Walter Roberson
2017-8-19
(Jan used to have a different profile picture. He also used to get a lot of email requests from people. One day I teased him that it was because he had such a handsome and elegant profile picture. Jan changed his picture to the funny face you see now. The number of email requests he got dropped considerably. ;-) )
John BG
2017-8-18
编辑:John BG
2017-8-18
Hi JB
thanks for pointing out the code supplied by gnovice.
I just tried it and, with gnovice characters like '@' are taken as numbers.
Let's say that there's s line like
'4103 0495@ 3725'
.
then
.
A={'1 TITLE 13122423';
'2 NAME Bob';
'10 PROVIDER James';
'44 234 456 234 345';
'48 344 454 462 435';
'4100 text';
'4101 text';
'4102 2more text';
'4103 0495@ 3725'}
index = ~any(cellfun(@any, isstrprop(A, 'alpha')), 2); C = A(index, :)
=
3×1 cell array
'44 234 456 234 345'
'48 344 454 462 435'
'4103 0495@ 3 725'
My answer does only take into account numbers, as requested
A={'1 TITLE 13122423';
'2 NAME Bob';
'10 PROVIDER James';
'44 234 456 234 345';
'48 344 454 462 435';
'4100 text';
'4101 text';
'4102 2more text';
'4103 0495@ 3725'}
B=[]; % log to record locations of lines to delete
L0='0123456789';
L1=' 0123456789';
for k=1:1:length(A)
L=A{k,:}
s1=1;while L(s1)~=' ' % start from left until find ' '
s1=s1+1;
end
while L(s1)==' ' % in case more than one consecutive ' '
s1=s1+1
end
while s1<length(L) && ~isempty(strfind(L1,L(s1)))
s1=s1+1; % shift pointer while ' ' or number
end
if isempty(strfind(L0,L(s1))) % check
B=[B k];
end
end
A(B)=[]
=
2×1 cell array
'44 234 456 234 345'
'48 344 454 462 435'
.
If you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
9 个评论
Steven Lord
2017-8-18
I recommend everyone except the original poster JB take a step back from this question for a little while. Remember that one of the tips for a helpful answer is "Be honest and considerate with all responses to all contributors." and a little breather might help make that easier.
JB, please add a comment to the original question indicating if you are satisfied with a combination of one or more of the responses you've received. If you aren't, clarify what aspect of the problem has not yet been solved. If you feel one of the answers was most useful in solving the problem, consider accepting it.
John BG
2017-8-18
Steven Lord
Your intervention is highly appreciated.
Too many comments often clutter the answers, and it may be the case that the question originator, here JB, decides just to walk away from such discussions, sometimes diverging to speed and code compactness accessory considerations, while it's not yet know whether JB finds any useful code at all, so far.
Awaiting JB response
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!