Azzera filtri
Azzera filtri

Concatenating a cell array

2 visualizzazioni (ultimi 30 giorni)
Devon
Devon il 5 Ott 2014
Commentato: Devon il 5 Ott 2014
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!

Risposta accettata

David Young
David Young il 5 Ott 2014
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 Commento
Devon
Devon il 5 Ott 2014
Sorry yeah, I did mean rows 1 and 4 from that data. Thanks a ton, this did exactly what I needed it to do!

Accedi per commentare.

Più risposte (1)

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

Categorie

Scopri di più su Data Type Identification in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by