Concatenating a cell array
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Devon
el 5 de Oct. de 2014
Comentada: Devon
el 5 de Oct. de 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!
0 comentarios
Respuesta aceptada
David Young
el 5 de Oct. de 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, :);
Más respuestas (1)
Guillaume
el 5 de Oct. de 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, :) = [];
0 comentarios
Ver también
Categorías
Más información sobre Data Type Identification en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!