Are there any function to check a string in the argument list?

For example, "off" is existed in the argument list or not?

2 Comments

Do you mean, is any argument in the function equal to a string 'off'? Or do you mean how to check if a particular argument was actually passed in and is equal to the string 'off'?
the first one, the position is not important

Sign in to comment.

 Accepted Answer

any(cellfun(@isequal,varargin,repmat({'off'},size(varargin))))

1 Comment

thanks, however I do not understand what is the problem with the previous one.
ismember(varargin,'off') gives back something like [0 0 0 1 0] isn't it?

Sign in to comment.

More Answers (1)

See this snippet:
ca = {'fubar', 'snafu', 'foobar', 'Off', 'off', 'office'}
% Case sensitive search
[ia, ib] = ismember(ca, 'off')
% Case insensitive search
[ia, ib] = ismember(lower(ca), 'off')
For you, the cell array ca would be "vargin" cell array.

2 Comments

I've tried this, but not working:
flag = any(ismember(varargin,'off'));
Error using cell/ismember>cellismemberR2012a (line 193) Input A of class cell and input B of class char must be cell arrays of strings, unless one is a string.
Error in cell/ismember (line 57) [varargout{1:max(1,nargout)}] = cellismemberR2012a(A,B);
Error in myfunction (line 14) flag = any(ismember(varargin,'off'));
Error in myscript (line 17) output = myfunction(1,2,5,'off',-15);
If one argument is not a string, then the test will fail. All need to be strings.

Sign in to comment.

Categories

Asked:

on 16 Sep 2015

Commented:

on 17 Sep 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!