help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Applying a function to selected element type in a mixed-type cell ar


From: Philip Nienhuis
Subject: Re: Applying a function to selected element type in a mixed-type cell array
Date: Tue, 22 May 2012 00:56:31 +0200
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.11) Gecko/20100701 SeaMonkey/2.0.6

Ben Abbott wrote:

On May 21, 2012, at 5:43 PM, Philip Nienhuis wrote:

Suppose a cell array like:

arr = {10, int32(15), 'Txt'; '', 25, true; NaN, 'String', single(100)}

How can all numeric values efficiently (w/o for loops etc.) be converted to 
double, leaving the other cell array contents untouched?

Getting at something like  "arr(find (cellfun ("isnumeric", [arr])))" is easy 
enough, but the follow-up is a hazy to me.

Thanks,

Philip

A Two-liner ?

        n = cellfun (@isnumeric, arr, "UniformOutput", true);
        arr(n) = cellfun (@double, arr(n), "UniformOutput", false);

Yep that'll work fast enough. I got close to something like the second line myself but I guess I forgot about the '"UniformOutput", false' thing.

BTW I need this to fix a bug in the io pkg spreadsheet writing scripts that turns out to have been there from the very beginning: The various Java and ActiveX methods that transfer numerical values to spreadsheets accept only doubles, no int<x>, single or unsigned. Not so strange when you realize that spreadsheets essentially contain just boolean, double or text values (cell formatting makes for a -cosmetic- wider variety). I never hit this limitation until today at work when I was converting text files containing integer values into spreadsheets.

Thanks Ben & Juan,

Philip


reply via email to

[Prev in Thread] Current Thread [Next in Thread]