2
votes

In my matlab workspace a table class contains cells with entries within them like this


'206'   '2000'  '.12750'    'N/A'   '36'    '116'   '2006-03-16 00:00:00'   '0'
'180'   '10000' '.10500'    'N/A'   '36'    '116'   '2006-03-13 00:00:00'   '0'

Each entry is a character, for example for '206', the entry is " '206' " and 206 is not a number but a character. How can I go through every single cell, take out quotes from the cell and convert the entries which are numbers into actual numbers?

2
Can you provide the code to create a minimal table like the one you have? - houtanb

2 Answers

0
votes

You can use table2cell to convert it to a cell. Let's call this cell array x; then use a regexp to detect numerics (I seem to recall that there's an easier way to do this step, but don't remember the exact function).

isNumeric = @( c) ~isempty( regexp(c,'^\d*$'))

after this , apply it to the whole cell array

idx = cellfun(isNumeric,x)

apply str2num to the cells that were numeric:

x(idx) = cellfun( @str2num , x(idx),'UniformOutput',false)
1
votes

There are str2num function in Matlab converts string to numeric data. So the only question - how to access the values from your table class.

If the problem to separate numeric data from, for example, some text you can use ismember function in this way:

a = '3';          %put here code of accessing your needed cell
b='0123456789';
ismember(a,b);    %return 1 if `a` is a member of `b`