Rank: 6925 based on 8 downloads (last 30 days) and 1 file submitted
photo

Christian

E-mail

Personal Profile:

 

Watch this Author's files

 

Files Posted by Christian
Updated   File Tags Downloads
(last 30 days)
Comments Rating
19 Aug 2013 String comparison The function searches for a string inside a given target and returns 1 for found or 0 for not found. Author: Christian string, compare, search 8 3
  • 2.0
2.0 | 1 rating
Comments and Ratings by Christian
Updated File Comments Rating
16 Aug 2013 String comparison The function searches for a string inside a given target and returns 1 for found or 0 for not found. Author: Christian

Thanks Richard and Jan for your comments. I included your suggestions and updated the code.

Comments and Ratings on Christian 's Files View all
Updated File Comment by Comments Rating
16 Aug 2013 String comparison The function searches for a string inside a given target and returns 1 for found or 0 for not found. Author: Christian Christian

Thanks Richard and Jan for your comments. I included your suggestions and updated the code.

15 Aug 2013 String comparison The function searches for a string inside a given target and returns 1 for found or 0 for not found. Author: Christian Jan Simon

It is essential for reliable programs, that they stop in case of errors. Therefore "error()" is much better than "disp()" in case of not matching inputs. When the lean documentation explains, that the search-term must be a string, accepting a cell string is not apropriate.

Richard's suggestion to use STRFIND is important. I'd suggest:

function result = strcompare(target,searchterm)
if ischar(target)
result = any(strfind(target, searchterm);
elseif iscellstr(target)
result = ~cellfun('isempty', strfind(target, searchterm));
else
error('1st input must be a string or cellstring');
end

15 Aug 2013 String comparison The function searches for a string inside a given target and returns 1 for found or 0 for not found. Author: Christian Richard Crozier

This can be replaced with

~isempty(strfind(str, substr))

or for your example

>> ~isempty(strfind('Marc goes fishing', 'goes'))

ans =

1

strfind also works for cell arrays

>> strfind({'Marc goes fishing', 'banafdsafas'}, 'goes')

ans =

[6.0000e+000] []

so could be adapted for your other use cases. It will be much faster than your loopy code, which is also inefficient in other ways.

Contact us