syntax "if"

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

syntax "if"

FelipeLyra
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 ?




--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: syntax "if"

Doug Stewart-4


On Tue, Jun 18, 2019 at 12:12 PM FelipeLyra <[hidden email]> 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 ?




--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


I think you want
Built-in Function: strcmp (s1s2)
Return 1 if the character strings s1 and s2 are the same, and 0 otherwise. 


--
DASCertificate for 206392



Reply | Threaded
Open this post in threaded view
|

Re: syntax "if"

apjanke-floss
In reply to this post by FelipeLyra


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 | Threaded
Open this post in threaded view
|

Re: syntax "if"

José Abílio Matos
In reply to this post by FelipeLyra
On Tuesday, 18 June 2019 15.55.48 WEST FelipeLyra wrote:
> 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 ?

This is a FAQ. :-)

To compare strings you need to use strcmp. For more details use

help strcmp

Regards,
--
José Matos




Reply | Threaded
Open this post in threaded view
|

Re: syntax "if"

BGreen
In reply to this post by FelipeLyra
I've never done string comparison before, but I noticed that "January" has seven characters while "February" has eight. It looks like they're being compared as character arrays and the comparison is failing because they're incommensurate.

The following workaround ran succesfully for me:

if length(mes)==7 && mes == 'january'
  a = 1;
  endif
if length(mes)==8 && mes == 'february'
  b = 2;
  endif
if length(mes)==5 && mes == 'march'
  c = 3;
  endif

Also, why not reduce the number of comparisons with elseif?

if length(mes)==7 && mes == 'january'
  a = 1;
elseif length(mes)==8 && mes == 'february'
  b = 2;
elseif length(mes)==5 && mes == 'march'
  c = 3;
end

- Brett Green


On Tue, Jun 18, 2019 at 12:12 PM FelipeLyra <[hidden email]> 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 ?




--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html




Reply | Threaded
Open this post in threaded view
|

Re: syntax "if"

shall689
In reply to this post by FelipeLyra
I think you should use strcmp(mes,'january') and etc instead of ==.



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: syntax "if"

FelipeLyra
In reply to this post by FelipeLyra
Thank you guys!

"strcmp" worked




--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html