필터 지우기
필터 지우기

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 CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by