4

In MATLAB I am trying to do a function on a cell array, but am not having much luck. I would like to create a cellfun which checks whether str2double returns NaN values and then perform the str2double on the values which aren't NaNs. I'm trying to use an anonymous function with an IF Else sort of statement in it but not really getting anywhere. Here is what I have come up with so far:

x = cellfun(@(x)~isnan(str2double(x)),str2double(x))

However it doesn't work, could anybody help me out?

7
  • What should be done to cells that do return NaN after calling str2double on them? Commented Apr 26, 2016 at 14:31
  • 1
    could you provide an example of that data you are trying to input. Commented Apr 26, 2016 at 14:31
  • An aside, you are using the same variable name to represent 3 different concepts here. x is the input, the output and also the variable of the anonymous function. I think this is likely to be very confusing. At least change the anonymous function variable name. Commented Apr 26, 2016 at 14:33
  • I'm very new to anonymous functions in MATLAB and am having trouble finding resources to tell me about it Commented Apr 26, 2016 at 14:35
  • 1
    It's just a general cell array where they are all strings but i want to convert some strings to doubles ie x = {'date','number';'12/12/12','3'}. I'd like to convert the 3 here to a double and return the cell array Commented Apr 26, 2016 at 14:41

3 Answers 3

6

Here is a nice, compact and working iif implementation:

iif = @(varargin) varargin{3-(varargin{1}>0)}

Usage:

iif(condition, true_value, false_value)

The function returns true value if the condition evaluates to true and the false_falue otherwise.

Here is a useful filter that can be applied on cells read from csv or excel file so they can be used as a numeric array. For example on an arrary Ra that was read using xlsread:

numeric_array = cellfun( @(x) iif(isnumeric(x) & ~isempty(x),x,NaN), Ra);
Sign up to request clarification or add additional context in comments.

Comments

3

You could use logical indexing:

x = {'1', 'NaN', '2', 'NaN'}
y = str2double(x(~isnan(str2double(x))))

y =
     1     2

This calls str2double twice, so it may run a little slow if you have to do it a million times.

EDIT: as pointed out by Dan, if you want to change the cell array in place, use

x{~isnan(str2double(x))} = str2double(x(~isnan(str2double(x))))

3 Comments

To completely match OP's attempt this should be x(~isnan(str2double(x))) = str2double(x(~isnan(str2double(x))))
@Dan: good point - my solution does not change the array in place.
This would work yes thank you. I should maybe say I am asking how to get the functionality of an If Else statement in a Anonymous function more clearly, this was kind of a general example (though I am still using it). Perhaps I am just barking up the wrong tree
1

You might be able to get this to work using Loren Shure's inline conditional:

iif = @(varargin) varargin{2 * find([varargin{1:2:end}], 1, 'first')}();

Then you can try

x = cellfun(@(y)iif(~isnan(str2double(y)), str2double(y), true, y), x, 'uni', 0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.