How to use cellfun in only cells with numbers or how to create a csv file with different elements (numbers and strings)?
Show older comments
I have a big cell array (22002x12) with multiple elements (strings and numbers) and I want to convert them to a CSV file but obviously I can't. So I was using cellfun to convert everything in either strings or numbers but is not possible.
So my question is if I have a cell array like this:
cellArray ={[ 1] '0.431089' '0.210426' '0.530389' '0.529967' '0.990659';
[ 2] '3.63584' '2.9894' '2.52868' '3.80065' '3.14567';
[ 3] [ 0] [ 0] '0.0593239' [ 0] '0.110771';
[ 9] '1.51064' '1.34827' '1.77354' '1.43471' '1.21254';
[ 10] '0.080619' [ 0] '0.0531488' '0.174925' '0.0496248';
[ 12] '0.129531' '0.169362' '0.170763' '0.21077' '0.0797323';
[ 13] '0.128055' '0.133946' '0.337633' '0.208367' '0.118236'}
how could I convert everything into numbers or cells using cellfun (or a better tool)?
I've been using :
matrix = cellfun(@str2num, cellArray)
Or how can I create a CSV file with this data?
I've been using :
csvwrite('new.csv',cellArray)
Thanks in advance!
Accepted Answer
More Answers (2)
Andrei Bobrov
on 4 May 2017
Edited: Andrei Bobrov
on 4 May 2017
l0 = cellfun(@ischar,cellArray);
cellArray(l0) = num2cell(str2double(cellArray(l0)));
or
l0 = cellfun(@isnumeric,cellArray);
cellArray(l0) = sprintfc('%d',[cellArray{l0}]);
Guillaume
on 4 May 2017
You would be better off fixing whichever code you've used that creates a mixture of numbers and strings in the same column. You would also be better off using tables instead of cell array.
Failing that:
function x = maybeconvert(x)
%convert x to number if it is a string, or leave it as is if already number
if ischar(x)
x = str2double(x); %don't use str2num, it's a very dangerous function
end
You can use that with cellfun easily:
m = cellfun(@maybeconvert, cellArray);
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!