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

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

2 commentaires

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

Connectez-vous pour commenter.

 Réponse acceptée

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

1 commentaire

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?

Connectez-vous pour commenter.

Plus de réponses (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 commentaires

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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by