Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Compare strings of different lengths without using strcmp?
Date: Fri, 25 Sep 2009 07:32:04 +0000 (UTC)
Organization: Universit&#228;t Heidelberg
Lines: 23
Message-ID: <h9hrlk$srn$1@fred.mathworks.com>
References: <1060919098.4446.1253822970612.JavaMail.root@gallium.mathforum.org>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1253863924 29559 172.30.248.35 (25 Sep 2009 07:32:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 25 Sep 2009 07:32:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 869888
Xref: news.mathworks.com comp.soft-sys.matlab:572825


Dear NouveauIX!

> I'm trying to compare two strings with if statements without using strcmp.
> 
> function log=myStrCmpI(char1,char2)
> c1=lower(char1);
> c2=lower(char2);
> if ?
>     log=true;
> else log=false;
> end

  function Log = myStrCmpI(char1,char2)
  Log = (numel(char1) == numel(char2)) && ...
           all(lower(char1) == lower(char2));
  return;

1. The && operator performs short-circuiting: the 2nd part of the comparison is not evaluated, if the 1st is FALSE already. This is important.
2. I've renamed "log" to "Log" to reduce conflicts with Matlab's logarithm function. Although this is not an error, it is worth to avoid as many confusions as possible...

Finally: STRCMPI is really fast and does check the types of the inputs. This can be a tremendous help for debugging! In addition STRCMPI works for cell strings also and cares about CHAR arrays with more than 1 row.

Good luck, Jan