matlab - Recognizing a cell input in one line -


consider following function inputs

>> b.a = 1     b =          a: 1  >> c = {'this' 'cell'}     c =          'this'    'cell'  >> d = [1 2 3]     d =          1     2     3 

the input can called in many ways example testfunction(b,d,c) testfunction(d,c,b) etc want cell input , retrieve data it

function testfunction(varargin) =1:numel(varargin)     if(iscell(varargin{i}))         fprintf('the input number %d cell!\n',i)     end end 

which recognizes if variable input cell there elegant way ? because iscell doesnt return index , used class() returns class of varargin instead of input

the main issue here not performance, rather readability , having pretty code.

i suggest create separate function checks cell is, , call function in main function. way can check cell is, in single line. simple, fast, , easy read. since can save function , close script in editor, calling builtin one-liner. function can other input checks.

example function:

function idx = cell_index(c) idx = 0; if isempty(c)     warning('no input given.') else     ii = 1:numel(c)         if(iscell(c{ii}))             idx = ii;         end     end end if idx == 0     warning('the input contained no cells.')         end end 

now can following in main function:

function output = main_function(varargin)     idx = cell_index(varargin)     fprintf('the input number %d cell!\n', idx)     %     % or fprintf('the input number %d cell!\n, cell_index(varargin))  % rest of code 

** loops versus other approaches:**

let's try out few functions:

appraoach 1: loop

this fastest one:

function s = testfunction1(varargin)     ii = 1:numel(varargin)     if(iscell(varargin{ii}))         s = sprintf('the input %i cell!\n', ii);     end end end 

approach 2: cellfun

this slowest , hardest read (imo):

function s = testfunction2(varargin)     if any(cellfun(@(x) iscell(x),varargin))     s = sprintf('the input %s cell\n', num2str(find(cellfun(@(x) iscell(x),varargin)))); end     end 

approach 3: cellfun

this easiest one, assuming don't want loops

function s = testfunction3(varargin)     x = find(cellfun(@(x) iscell(x),varargin)); if ~isempty(x)     s = sprintf('the input %i cell \n',x); end     end 

the following benchmarking performed using matlab r2014b, prior new jit engine!

f = @() testfunction1(b,c,d); g = @() testfunction2(b,c,d); h = @() testfunction3(b,c,d); timeit(f) ans =    5.1292e-05  timeit(g) ans =    2.0464e-04  timeit(h) ans =    9.7879e-05 

summary:

if want use loop-free approach, suggest last approach (the second cellfun version). performs 1 find , 1 call cellfun. it's therefore easier read, , it's faster.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -