No BSD License  

Highlights from
Cell 'Find And Replace'

3.33333

3.3 | 3 ratings Rate this file 13 Downloads (last 30 days) File Size: 1.84 KB File ID: #10847

Cell 'Find And Replace'

by Yoav Mor

 

24 Apr 2006 (Updated 26 Apr 2006)

Finds all occurrences of NaN in a cell array and replaces them.

| Watch this File

File Information
Description

cellData=cellNaNReplace (cellData, replaceWith)

Accepts a cell array and a char/integer and replaces all occurrences of NaN in that cell array with the 'replaceWith' value. This is especially useful when using 'readfromexcel' or other excel importers since they sometimes put NaN instead of empty cells.

Example of usage:
a = num2cell([0 1 2 NaN 4]);
b = cellNaNReplace (a,'');

or, if you're dealing with excel spreadsheets, you can use:

Data = readfromexcel(File,'All');
Data = cellNaNReplace (Data,0);

Enjoy.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
readfromexcel

MATLAB release MATLAB 7.1.0 (R14SP3)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
08 Aug 2006 Sebastian Walderich

Hi Yoav,

Good job, thanks for that! But maybe some annotations ;-)
For cell-arrays including numbers only, you can simply do something like this:
a = {1 2 NaN; 3 NaN 4; NaN 5 6};
a(isnan([a{:}])) = {-1}

Your script doesn't handle cell-arrays within cell-arrays, so I simplfied it to this:

--
function cellData = cellNaNReplace (cellData, replaceWith)
    if nargin ~= 2
        error(['function cellNaNReplace does not take ' nargin ' arguments']);
    elseif ~iscell(cellData)
        error(['first argument must be a cell array']);
    end

    cellData = cellfun(@cellrep, cellData, 'UniformOutput', false);

    function value = cellrep(num)
        if ~iscell(num) & isnan(num)
            value = replaceWith; % the replaced value
        else
            value = num; % the original number back.
        end
    end
end
--

There's no need for your parameter-trick. Nested subfunctions can see their parents variables.

Again, thanks for the script!

18 Apr 2007 Arya Bolurfrushan

Hello,

This is a great function but I get into this error constantly "??? Incorrect number of inputs.
On line 57 ==> cellData = cellfun(@cellrep, cellData, 'UniformOutput', false);"

Please help

21 Sep 2007 fan houjun

thanks

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
structures Yoav Mor 22 Oct 2008 08:23:01
cell arrays Yoav Mor 22 Oct 2008 08:23:01
cell Yoav Mor 22 Oct 2008 08:23:01
nan Yoav Mor 22 Oct 2008 08:23:01
find Yoav Mor 22 Oct 2008 08:23:01
search Yoav Mor 22 Oct 2008 08:23:01
replace Yoav Mor 22 Oct 2008 08:23:01
cell array Yoav Mor 22 Oct 2008 08:23:01

Contact us at files@mathworks.com