help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: strcmp not functioning as expected


From: Philip Nienhuis
Subject: Re: strcmp not functioning as expected
Date: Sun, 13 Mar 2011 05:05:16 -0700 (PDT)

CanadaPhil wrote:
> 
> Hello,
> I am using the following code to query the first column of a cell array
> for unique values and add them to a new cell array.  I am using strcmp to
> determine if the last unique string is the same as the first entry on the
> currect row.  The code does not run properly though because the return of
> strcmp is a matrix (clearly not what I want).  How can I correct my
> problem with strcmp?  
> Thanks,
> Phil
> 
> 
> function Indicators()
> Indi={"EMA","Buy on Price Cross","EMA Period","-","-","-","-","-";
> "EMA","Sell on Price Cross","EMA Period","-","-","-","-","-";
> "MACD","Buy on EMA Cross","Fast EMA Period","Slow EMA
> Period","-","-","-","-";
> "MACD","Sell on EMA Cross","Fast EMA Period","Slow EMA
> Period","-","-","-","-";};
> 
> AvailIndi={Indi{1,1}}
> LastIndi=Indi{1,1}
> IndiLine=2
> AvailIndiLine=2
> 
> while (IndiLine<=rows(Indi))
> disp("Indi{IndiLine,1}=");
> disp(Indi{IndiLine,1});
> disp("LastIndi=");
> disp(LastIndi);
> disp("strcmp(Indi{IndiLine,1},LastIndi)=");
> disp(strcmp(Indi{IndiLine,1},LastIndi));
>   if (strcmp(Indi{IndiLine,1},LastIndi)=1)
> 

Without sample data I find it hard to follow what happens, but this ^^^^
last line is clearly in error:

1. In general you'd rather use "if (strcmp (a, b) == 1)"  
(or even "if (strcmp (a, b))" )
Think of assignment (" = ") vs. comparison (" == ")

2. When you use functions that can return arrays (e.g., strmatch), tests
like below may come in handy:
if (isempty (strmatch (a, b)))
   .....
endif

3. Use spaces / whitespace within statements, that makes code more readable
and spotting typos much easier.

Philip


--
View this message in context: 
http://octave.1599824.n4.nabble.com/strcmp-not-functioning-as-expected-tp3350509p3351779.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]