Python

Convert cell arrays element-wise (cellfun)

Apply a function to each cell element

X = cellfun(@function , CellArray)

Convert cell array into matrix

Convert numbers from text string cell-array 'C' into a numerical matrix 'X'

C = {'11','12','13','14','15';...

     '21','22','23','24','25'}

X = cellfun(@str2num, C )

    11    12    13    14    15

    21    22    23    24    25

Convert subset of cell-array into matrix

X = cellfun(@str2num, C(2 , 1:3) )   %  elements 1...3 of second row

    21    22    23

X = cellfun(@str2num, C(: , 3) )     %  convert 3rd column

    13

    23


Convert vector into cell array of strings

Convert numerical vector into cell array of text strings (simple solution)

x = [ 11, 12, 13, 14, 15 ]  % row vector (not column vector!)

C = strsplit(num2str(x))

  {'11'}  {'12'}  {'13'}  {'14'}  {'15'}

Convert numerical vector into cell array of text strings (cellfun-style solution)

x = [ 11, 12, 13, 14, 15 ]  % row vector

    11    12    13    14    15

xc  = num2cell(x) % covert into (numerical) cell vector

  {[11]}  {[12]}  {[13]}  {[14]}  {[15]}

C = cellfun(@num2str , xc , 'UniformOutput', false)  % convert into strings

  {'11'}  {'12'}  {'13'}  {'14'}  {'15'}


Multiple inputs and custom functions

X = cellfun(@(x) myfunction(x,y) , CellArray)


One-liner: small custom functions inside cellfunc

% same x>12 check as before, but as short version inside the cellfun 

% find all text-numbers larger than 12 (convert string into numerical values and compare)

C = {'11','12','13','14','15';...

     '21','22','23','24','25'}

X = cellfun(@(x) str2num(x)>12 , C )

   0   0   1   1   1

   1   1   1   1   1

% convert string into numbers and divide by 100

 X = cellfun(@(x) str2num(x)/100 , C )

    0.1100    0.1200    0.1300    0.1400    0.1500

    0.2100    0.2200    0.2300    0.2400    0.2500

% add prefix

X = cellfun(@(x) {['T_',x]}, C )

    {'T_11'}  {'T_12'}  {'T_13'}  {'T_14'}  {'T_15'}

    {'T_21'}  {'T_22'}  {'T_23'}  {'T_24'}  {'T_25'}

% replace space with underscore for all elements in cell

C = {'A 1','A 2','A 3'};

X = cellfun(@(x) {strrep(x,' ','_')} , C)

  {'A_1'}  {'A_2'}  {'A_3'}

% replace underscores '_' in all cell fields to backslash underscore '\_' 

% using backslash to mask underscore from interpretation in plots (title, xlable,..)

SampleIDs = {'A_1','A_2','A_3'};

SampleIDs = cellfun(@(x) {strrep(x,'_','\_')} , SampleIDs)

 {'A\_1'}  {'A\_2'}  {'A\_3'}


Example of customized function

% define custom function

function c = myfunction(s,th)

    % convert string into numerical value and check if larger than threshold 'th'

    x = str2num(s);

    if isempty(x), x=NaN; end

    c = x > th;

%  apply custom function to each cell array (check if text numbers are larger than 12)

C = {'11','12','13','14','15';...

     '21','22','23','24','25'}

X = cellfun(@(x) myfunction(x,12) , C )

   0   0   1   1   1

   1   1   1   1   1

Filter (select) text of cell elements

Get cell elements containing specific text (keywords or substring)

% get index location of  'Control' samples

C = {'A1','A2','A3','','B1','B2','B3','','Control','Control','Control'};

id = find( cellfun(@(x) isequal(x,'Control'), C) )

   9   10   11

% get location of all cell elements that contain substring 'B' (find cells whose output of strfind is not ('~') empty)

id = find( cellfun(@(x) ~isempty(strfind(x,'B')), C) )

   5   6   7

C(id)

   {'B1'}  {'B2'}  {'B3'}

% get index of cells that starts with 'CTR' (control) at the first 3 letters

C={'CTR_2','TIME_1','TIME_2','TIME_3','CTR_2','TIME_4','TIME_5'}

idx = cellfun(@(x) isequal('CTR',x(1:3)), C )

   1   0   0   0   1   0   0

find(idx)

    1   5

Replace all text elements 'Control' with 'CTR'

% get index location of  'Control' samples and overwrite with 'CTR'

C = {'A1','A2','A3','','B1','B2','B3','','Control','Control','Control'};

C( find(cellfun(@(x) isequal(x,'Control'), C)) ) = {'CTR'}

  {'A1'} {'A2'} {'A3'} {0×0 char} {'B1'} {'B2'} {'B3'} {0×0 char} {'CTR'} {'CTR'} {'CTR'} 

see also:  → How to assign a single value to multiple cells

Find and remove empty cell elements

C = {'A1','A2','A3','','B1','B2','B3','','Control','Control','Control'};

% find all empty elements in cell array

idx = cellfun(@isempty, C)

     0   0   0   1   0   0   0   1   0   0   0

find(idx)

     4     8

% get subset containing all elements that are not ('~') empty

X = C( ~cellfun(@isempty, C) )

  {'A1'}  {'A2'}  {'A3'}  {'B1'}  {'B2'}  {'B3'}  {'Control'}  {'Control'}  {'Control'}


https://mathworks.com/help/matlab/ref/cellfun.html