help-octave
[Top][All Lists]
Advanced

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

Re: syntax "if"


From: Brett Green
Subject: Re: syntax "if"
Date: Tue, 18 Jun 2019 12:54:16 -0400

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 <address@hidden> 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 via email to

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