Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how to terminate a function?

Subject: how to terminate a function?

From: Abo Bshire

Date: 15 Nov, 2005 08:16:51

Message: 1 of 5

How can I terminate a function in a certain point?
for example, assume:

function a=myfun(b)
 
  for i=1:b
    i=i+1;
     % if i=3
     % a=i;
     % terminate the function
     % otherwise go on
  end
  a=i;

Subject: how to terminate a function?

From: John

Date: 15 Nov, 2005 14:42:03

Message: 2 of 5

try return or break



"Abo Bshire" <moh054@yahoo.com> skrev i en meddelelse
news:ef1b760.-1@webx.raydaftYaTP...
> How can I terminate a function in a certain point?
> for example, assume:
>
> function a=myfun(b)
>
> for i=1:b
> i=i+1;
> % if i=3
> % a=i;
> % terminate the function
> % otherwise go on
> end
> a=i;

Subject: how to terminate a function?

From: Abo Bshire

Date: 15 Nov, 2005 09:24:56

Message: 3 of 5

John wrote:
>
>
> try return or break

Yes that's correct ...
My mistake was trying to use return with the output name like :
return a
the correct sentax of return is just typing the command itself. SO,
my code is:
function a=myfun(b)
  for i=1:b
    i=i+1;
    if i=3
      a=i;
      return
    end
  a=i;
  end;

The break is used only to terminate the execution of for or while
loops.

thanks

Subject: how to terminate a function?

From: Jerome

Date: 16 Nov, 2005 03:35:39

Message: 4 of 5

Abo Bshire wrote:
>
> function a=myfun(b)
> for i=1:b
> i=i+1;
> if i=3
> a=i;
> return
> end
> a=i;
> end;
>

Hi,

The above code is wrong !
Are you new to Matlab ?
Is it just an example ?

Jérôme

Subject: how to terminate a function?

From: Abo Bshire

Date: 16 Nov, 2005 11:33:13

Message: 5 of 5

Jérôme wrote:
>
>
> Abo Bshire wrote:
>>
>> function a=myfun(b)
>> for i=1:b
>> i=i+1;
>> if i=3
>> a=i;
>> return
>> end
>> a=i;
>> end;
>>
>
> Hi,
>
> The above code is wrong !
> Are you new to Matlab ?
> Is it just an example ?
>
> Jérôme
Yes absolutley it was wrong ... specifically,
 'if i=3' should be 'if i==3'
it was just an example ...

Thanks,
Abo Bshire

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us