Find string in cell array with elements of different types

时光总嘲笑我的痴心妄想 提交于 2019-12-02 04:42:15

You can use the built-in function strcmp which should be fairly fast:

idx  = strcmp(str_to_find, varargin);

and that will give you an index to all cell elements that are strings matching the target.

Then, for pruning those elements, you can use

varargin( or(idx, [0 idx(1:end-1)]) ) = [];

assuming that idx is a row array.

Finally, you may also want to run some format checks to make sure that the user has not entered argument-pairs in the wrong order (or with an argument name that matches the parameter name) otherwise this kind of code will behave strangely.

What about trying this:

index  = find(strcmp(str_to_find, varargin));

This should give the index of 'parameter' and adding one to it will get the index of its 'value'

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!