help-octave
[Top][All Lists]
Advanced

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

Re: syntax "if"


From: Andrew Janke
Subject: Re: syntax "if"
Date: Tue, 18 Jun 2019 12:33:06 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.7.0


On 6/18/19 10:55 AM, FelipeLyra wrote:
> I'm trying to make a simple code using "if" :
> 
> clear;
> clc
> 
> dia = input('Day: ');
> printf("\n") #para pular linha
> 
> mes = input('Month: ','s');
> mes = tolower(mes);
> printf("\n")
> 
> #beta = input('Inclinação do modulo: ');
> #printf("\n")
> 
> lat = input('Latitude: ');
> printf("\n")
> 
> gsc = 1367;
> 
> if mes == 'january'
>   a = 1;
>   endif
> if mes == 'february'
>   b = 2;
>   endif
> if mes == 'march'
>   c = 3;
>   endif
> 
> and is giving the following error when the month is February or March:
> 
> error: teste2: mx_el_eq: nonconformant arguments (op1 is 1x8, op2 is 1x7)
> error: called from
>     teste2 at line 19 column 1
> 
> has anyone ever experienced this ?
> 

You can't use == to compare strings. The == operator does elementwise
comparison, but strings are arrays (of chars), and you want to test
whether the whole array matches another whole array. The error happens
when mes and the comparison string are of different lengths.

Use isequal() or strcmp() instead:

if isequal(mes, 'january')

if strcmp(mes, 'january')


Or replace all those if/endifs with a switch statement.

Cheers,
Andrew



reply via email to

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