File Exchange

image thumbnail


version (1.89 KB) by Ben Barrowes
Applied relational operators to such as == or < to character arrays or cell arrays of strings accord


Updated 18 Oct 2007

View License

% function out=strrel(in1,in2,relop)
% Compares strings according to strcmp and issorted according to relop
% in1 and in2 can be character arrays or cell arrays
% relop can be <, <=, >, >=, ==, and ~=, or their string equivalents
% such as 'ne' or 'gt', etc.
% Examples:
% c = {'How','much','wood','would','a','woodchuck','chuck?'};
% s = 'wood';
% r = strrel(s,c,'<')
% r = strrel(s,c,'gt')

Cite As

Ben Barrowes (2020). strrel (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (3)

Ahmed Fasih

issortedcellchar not found in 2012a.

Urs (us) Schwarz

since you seem to listen to your customers, i now show you the fastest solution, which will drop the execution time to ~25% of your current version:

% the crucial part of the engine
ie =strcmp(in1,in2);
for i=1:numel(in1)
is=issortedcellchar({in1{i},in2{i}}); %!!!
switch lower(relop)
case {'<','lt'}
out(i)=is & ~ie(i);
case {'<=','le'}
out(i)=is | ie(i);
case {'==','eq'}
case {'~=','ne','!=','/='}
case {'>','gt'}
out(i)=~is & ~ie(i);
case {'>=','ge'}
out(i)=~is | ie(i);
error([relop,' is not recognized'])

also, being an academic yourself, you certainly know how to acknowledge the contributions of others to your work, which may not simply be a SMALL BUGFIX as mentioned earlier...


urs (us) schwarz

nice idea... a few remarks
1) the help section needs a bit of work, eg, H1 and syntax
2) the last switch statement should read GE instead of GT
3) the engine could be vectorized, eg
t={the token};
c=the cellstring;
is=cellfun(@(x) issorted([t,{x}]),c);
% the swith, eg, 'gt'
out=~is & ~ie

just some thoughts


Faster due to issortedcellchar as suggested by Urs (us) Schwarz.

Small bugfix.

MATLAB Release Compatibility
Created with R2006a
Compatible with any release
Platform Compatibility
Windows macOS Linux