フィルターのクリア

Concatenating a cell array

2 ビュー (過去 30 日間)
Devon
Devon 2014 年 10 月 5 日
コメント済み: Devon 2014 年 10 月 5 日
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!

採用された回答

David Young
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 件のコメント
Devon
Devon 2014 年 10 月 5 日
Sorry yeah, I did mean rows 1 and 4 from that data. Thanks a ton, this did exactly what I needed it to do!

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

その他の回答 (1 件)

Guillaume
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, :) = [];

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by