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

10 views (last 30 days)
For example, "off" is existed in the argument list or not?
  2 Comments
James Tursa
James Tursa on 16 Sep 2015
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'?

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 16 Sep 2015
any(cellfun(@isequal,varargin,repmat({'off'},size(varargin))))
  1 Comment
Mr M.
Mr M. on 17 Sep 2015
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)

Image Analyst
Image Analyst on 16 Sep 2015
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
Mr M.
Mr M. on 16 Sep 2015
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);
Image Analyst
Image Analyst on 17 Sep 2015
If one argument is not a string, then the test will fail. All need to be strings.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!