Hello,
I have a table (only showing one column here for demo purposes):
T = table({'147956, 154414'; '1, 7439'; '93053, 101815'; '50151, 54585; 827532, 828570; 5846728, 5848716'; '1063488, 1079019'},'VariableNames',{'indices'})
What I need to do is to convert the 1×1 char arrays into 1xn double arrays.
First I tried to use varfun to apply the str2double function to the indices variable:
T.indices = varfun(@str2double,T,'InputVariables','indices')
That ran without error, but converted all of my 1×1 char arrays into 1×1 double arrays with NaN in each.
I tried the same thing with str2num:
T.indices = varfun(@str2num,T,'InputVariables','indices')
But it gave me the error:
Applying the function 'str2num' to the variable 'indices' generated the following error:
Input must be a character vector or string scalar.
What ultimately worked was using cellfun to apply the str2num function:
T.indices = cellfun(@str2num,T.indices,'UniformOutput',false)
But I'm not sure why. What is the difference between passing "T,'InputVariables','indices'" into varfun and passing "T.indices" into cellfun? In what case would you use varfun? And is there any way to pass in my variable so that str2double returns the correct output?
Thanks in advance for any insights,
Rob
Best Answer