Concatenating a cell array
2 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
I'm trying to take a large cell array and cut out any rows that have the letters in the first column. This is from an event file for baseball games, and a small example:
'fox-a001' 'Andy Fox' ''
'5' '1' 'K'
'6' '0' 'NP'
'kim-s001' 'Sun-Woo Kim' ''
'6' '0' '8/F'
I would like to delete any rows where the first column isn't a number (for example, the rows 1 and 5 in this example). Since the entire data set is about 12,000 rows long, ideally having a script that can find these values and remove then is ideal. I tried logical operators and ran into errors that the functions wouldn't work with cell arrays, so I'm not sure what to try next. Thanks for any help!
0 个评论
采纳的回答
David Young
2014-10-5
I guess you mean that you would like to delete rows 1 and 4. If that's right, here's one way:
% set up the test data
data = {'fox-a001' 'Andy Fox' ''; ...
'5' '1' 'K'; ...
'6' '0' 'NP'; ...
'kim-s001' 'Sun-Woo Kim' ''; ...
'6' '0' '8/F'};
% function that defines what is meant by a number. This says a string
% represents a number if every character is a digit, or . or + or -, but
% this can be modified according to what is right for your data.
isnum = @(s) all(ismember(s, '0123456789.+-'));
% use the function to find the row indices where the first entry is a
% number
numrows = cellfun(isnum, data(:,1));
% make a new matrix, retaining only those rows
dataNumsOnly = data(numrows, :);
更多回答(1 个)
Guillaume
2014-10-5
I would just use str2double to test if the string is a number. It will return NaN if not. Perform the test on the first column of the cell array:
c = {
'fox-a001' 'Andy Fox' ''
'5' '1' 'K'
'6' '0' 'NP'
'kim-s001' 'Sun-Woo Kim' ''
'6' '0' '8/F'};
tf = isnan(str2double(c(:, 1)));
c(tf, :) = [];
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!