loop-less way to str2double a portion of a cell array in place?

Hi! I feel like I shouldn't need to ask this by now, but everyway I try doesn't work: I have a cellstr array, some elements of which are character representations of numbers, e.g., A = {'0', 'hello', '2'}; I want to convert this to A = {0, 'hello', 2}. I've tried dozens of variations of: A(~isnan(str2double(A))) = str2double(A(~isnan(str2double(A)))) (I actually have a separate indexing arry, I'm just using isnan for illustrative purposes, but it does extract the sub-array I want to convert) involving {} instead of () in various places, cellfun, arrayfun, deal, mat2cell, cell2mat, etc., and everything either returns an error or assigns the whole target sub-array to every target array element, i.e., I get A = {{0,2}, 'hello', {0,2}}. Is there a loop-less way to convert {'0', 'hello', '2'} to {0, 'hello', 2}? Thanks!
DG

 Accepted Answer

isnan(cellfun(@str2double,your_cell));

4 Comments

>> A = {'0', 'hello', '2'}
A =
'0' 'hello' '2'
>> isnan(cellfun(@str2double,A))
ans =
0 1 0
This just indexes the array, which I have no problem doing: did you understand the question? Did you try your Answer before posting?
I think Sean meant this
idx=~isnan(cellfun(@str2double,A));
A(idx)=cellfun(@str2double,A(idx),'uni',0),
but note my Answer as well. There is no speed advantage to a cellfun solution over a straight up for-loop.
I was pointing you in the right general direction, not giving you complete code. Also, the question is formatted as a wall of text which is kind of hard to follow.
your_cell = {'0','hello','1'};
attempt = cellfun(@str2double,your_cell);
idx = ~isnan(attempt);
your_cell(idx) = num2cell(attempt(idx))
Of course, if I had to do this I would probably just write a function
function x = foo(x)
y = str2double(x)
if ~isnan(y)
x = y;
end
And then:
cellfun(@foo,...)

Sign in to comment.

More Answers (2)

Matt J
Matt J on 24 Oct 2012
Edited: Matt J on 24 Oct 2012
In general, there is no loop-less way to manipulate cell arrays at all.
cellfun, arrayfun, mat2cell, cell2mat,etc... all use loops internally.
It shouldn't matter. If you're using cells big enough for for-loop speed to matter, you're probably not using cell arrays as they're intended.

4 Comments

Sorry, I didn't mean loop-less in a "deep" sense, merely in a code sense, i.e., I simply want a way to do this without having to write an explicit for loop (which is eminently do-able on cell arrays in general: examples on this list abound, and this is essentially the very function of the cellfun function). As for your editorializing, I do not understand what you are trying to say: that I shouldn't be using cellarrays if some of the elements are very large? In my particular case, this is not so (the example I provided reflects the order of magnitude of the size of the cellarray elements and the overall size of the cell array I'm dealing with). But again, speed is not the issue, code overhead is (I feel that having to use an explicit for loop for something as simple as this on an object as small as those I'm dealing with is excessive). Next!
Yes, but cellfun tends to be slower than a well written for-loop anyway. It's really only for the cosmetic style of your code.
No, I didn't say anything about the cell elements being large. I meant that if numel(thecellarray) is really large it means you've stored a lot of data discontiguously in memory. No manipulation you do with such an array can be fast, with for-loops or otherwise. So, when speed is your priority and the data is large, you just don't use cells.
So I apparently am not using cell arrays correctly. Am using the approach:
mat = cell2mat(cellfun(@str2double,your_cell));
to create 'mat' which is 13x18000, there is a bunch of code predicated on having this array. It works fine for a few files, but now I'm opening thousands of files and it is very slow (~20sec each file).
I've tried to use fscanf but cannot seem to get the array correctly formated. The input files are comma delimited, with a 4 line header and the initial column a timestamp eg "yyyy-mm-dd etc".

Sign in to comment.

Thanks, Matt, your example works. In my case, I needed more than simply being "pointed in the right direction": as I said in the OP, I had no trouble extracting the indices of the elements to convert (nor actually converting those elements); it was the "end game" of assigning the results of this extraction and conversion to their original locations in the original cell array that was eluding me. So thanks for providing a consise, complete solution.

Categories

Products

Community Treasure Hunt

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

Start Hunting!