Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: go to statment in matlab

Subject: go to statment in matlab

From: huda nawaf

Date: 09 Aug, 2007 12:11:10

Message: 1 of 14

how I can use go to statment in matlab?

Subject: Re: go to statment in matlab

From: us

Date: 09 Aug, 2007 12:27:44

Message: 2 of 14

huda nawaf:
<SNIP stubborn...

> how I can use go to statment in matlab?

the <goto> statement does not exist in ML...

us

Subject: Re: go to statment in matlab

From: someone

Date: 09 Aug, 2007 18:13:06

Message: 3 of 14

"huda nawaf" <halmamory@yahoo.com> wrote in message
<f9f08u$qqp$1@fred.mathworks.com>...
> how I can use go to statment in matlab?


See:

<http://blogs.mathworks.com/loren/2006/04/01/goto-coming-at-
long-last-vectorized-no-less/>


Subject: Re: go to statment in matlab

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 09 Aug, 2007 18:26:31

Message: 4 of 14

In article <f9f180$f8j$1@fred.mathworks.com>, us <us@neurol.unizh.ch> wrote:
>huda nawaf:
><SNIP stubborn...

>> how I can use go to statment in matlab?

>the <goto> statement does not exist in ML...

*Some* uses of goto can be replaced by try/catch blocks
catching custom error() conditions.
--
  Prototypes are supertypes of their clones. -- maplesoft

Subject: Re: go to statment in matlab

From: huda nawaf

Date: 18 Aug, 2007 05:50:39

Message: 5 of 14

"someone " <someone@somewhere.net> wrote in message
<f9flfi$t5f$1@fred.mathworks.com>...
> "huda nawaf" <halmamory@yahoo.com> wrote in message
> <f9f08u$qqp$1@fred.mathworks.com>...
> > how I can use go to statment in matlab?
>
>
> See:
>
> <http://blogs.mathworks.com/loren/2006/04/01/goto-coming-
at-
> long-last-vectorized-no-less/>
>
>
hi someone,
I cant access to this web.
if you have another please give it.
thanks,huda

Subject: Re: go to statment in matlab

From: huda nawaf

Date: 18 Aug, 2007 05:53:49

Message: 6 of 14

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <f9fm8n$7g1$1@canopus.cc.umanitoba.ca>...
> In article <f9f180$f8j$1@fred.mathworks.com>, us
<us@neurol.unizh.ch> wrote:
> >huda nawaf:
> ><SNIP stubborn...
>
> >> how I can use go to statment in matlab?
>
> >the <goto> statement does not exist in ML...
>
> *Some* uses of goto can be replaced by try/catch blocks
> catching custom error() conditions.
> --
> Prototypes are supertypes of their clones. --
 maplesoft
hi us,
please I don't know how can use it (try and catch)
please give me example .
thanks

Subject: Re: go to statment in matlab

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 18 Aug, 2007 06:44:44

Message: 7 of 14

In article <fa61hd$so$1@fred.mathworks.com>,
huda nawaf <halmamory@yahoo.com> wrote:
>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
>message <f9fm8n$7g1$1@canopus.cc.umanitoba.ca>...
>> In article <f9f180$f8j$1@fred.mathworks.com>, us
><us@neurol.unizh.ch> wrote:
>> >huda nawaf:
>> ><SNIP stubborn...

>> >> how I can use go to statment in matlab?

>> >the <goto> statement does not exist in ML...

>> *Some* uses of goto can be replaced by try/catch blocks
>> catching custom error() conditions.

>hi us,

'us' was the person who mentioned that goto does not exist.
The person you are replying to, who mentioned try and catch,
is 'Walter'.

>please I don't know how can use it (try and catch)
>please give me example .


%code as it would be if GO TO existed:
for K = 1:N
  do_something;
  if some_variable == 0, GO TO Opps0, end;
  do_something_more;
end
return

Opps0:
sprintf('Zero encountered at iteration %d\n', K);
return


%code using try/catch instead:
try
  for K =1:N
    do_something;
    if some_variable == 0 then error('Opps0') end;
    do_something_more;
  end
  return
catch
  if lasterror == 'Opps0'
    sprintf('Zero encountered at iteration %d\n', K);
    return
  else
    rethrow(lasterror)
  end
end
--
   "No one has the right to destroy another person's belief by
   demanding empirical evidence." -- Ann Landers

Subject: Re: go to statment in matlab

From: huda nawaf

Date: 18 Aug, 2007 08:57:08

Message: 8 of 14

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fa64gs$d9m$1@canopus.cc.umanitoba.ca>...
> In article <fa61hd$so$1@fred.mathworks.com>,
> huda nawaf <halmamory@yahoo.com> wrote:
> >roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
> >message <f9fm8n$7g1$1@canopus.cc.umanitoba.ca>...
> >> In article <f9f180$f8j$1@fred.mathworks.com>, us
> ><us@neurol.unizh.ch> wrote:
> >> >huda nawaf:
> >> ><SNIP stubborn...
>
> >> >> how I can use go to statment in matlab?
>
> >> >the <goto> statement does not exist in ML...
>
> >> *Some* uses of goto can be replaced by try/catch
blocks
> >> catching custom error() conditions.
>
> >hi us,
>
> 'us' was the person who mentioned that goto does not
exist.
> The person you are replying to, who mentioned try and
catch,
> is 'Walter'.
>
> >please I don't know how can use it (try and catch)
> >please give me example .
>
>
> %code as it would be if GO TO existed:
> for K = 1:N
> do_something;
> if some_variable == 0, GO TO Opps0, end;
> do_something_more;
> end
> return
>
> Opps0:
> sprintf('Zero encountered at iteration %d\n', K);
> return
>
>
> %code using try/catch instead:
> try
> for K =1:N
> do_something;
> if some_variable == 0 then error('Opps0') end;
> do_something_more;
> end
> return
> catch
> if lasterror == 'Opps0'
> sprintf('Zero encountered at iteration %d\n', K);
> return
> else
> rethrow(lasterror)
> end
> end
> --
> "No one has the right to destroy another person's
belief by
> demanding empirical evidence." -- Ann
Landers
hi Roberson,
thanks ,
When I running this example . The following message error
appeared:
??? Error: File: e:\MATLAB6p5\work\us.m Line: 4 Column: 29
Missing operator, comma, or semicolon.


Subject: Re: go to statment in matlab

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 18 Aug, 2007 16:38:59

Message: 9 of 14

In article <fa6c94$70h$1@fred.mathworks.com>,
huda nawaf <halmamory@yahoo.com> wrote:
>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
>message <fa64gs$d9m$1@canopus.cc.umanitoba.ca>...

>> %code using try/catch instead:
>> try
>> for K =1:N
>> do_something;
>> if some_variable == 0 then error('Opps0') end;
>> do_something_more;
>> end
>> return
>> catch
>> if lasterror == 'Opps0'
>> sprintf('Zero encountered at iteration %d\n', K);
>> return
>> else
>> rethrow(lasterror)
>> end
>> end

>hi Roberson,

"Roberson" is my family name, not my personal name.


>When I running this example . The following message error
>appeared:
>??? Error: File: e:\MATLAB6p5\work\us.m Line: 4 Column: 29
>Missing operator, comma, or semicolon.

You weren't meant to literally run it! It was an outline, nothing more!!

But the problem was that I've been using other programming
languages lately, so I accidently used 'then' instead of the Matlab
equivilent structure:

  if some_variable == 0, error('Opps0'), end
or
  if some_variable == 0
    error('Opps0')
  end

--
Programming is what happens while you're busy making other plans.

Subject: Re: go to statment in matlab

From: huda nawaf

Date: 21 Aug, 2007 08:37:11

Message: 10 of 14

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fa77b3$3pt$1@canopus.cc.umanitoba.ca>...
> In article <fa6c94$70h$1@fred.mathworks.com>,
> huda nawaf <halmamory@yahoo.com> wrote:
> >roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
> >message <fa64gs$d9m$1@canopus.cc.umanitoba.ca>...
>
> >> %code using try/catch instead:
> >> try
> >> for K =1:N
> >> do_something;
> >> if some_variable == 0 then error('Opps0') end;
> >> do_something_more;
> >> end
> >> return
> >> catch
> >> if lasterror == 'Opps0'
> >> sprintf('Zero encountered at iteration %d\n', K);
> >> return
> >> else
> >> rethrow(lasterror)
> >> end
> >> end
>
> >hi Roberson,
>
> "Roberson" is my family name, not my personal name.
>
>
> >When I running this example . The following message
error
> >appeared:
> >??? Error: File: e:\MATLAB6p5\work\us.m Line: 4 Column:
29
> >Missing operator, comma, or semicolon.
>
> You weren't meant to literally run it! It was an
outline, nothing more!!
>
> But the problem was that I've been using other
programming
> languages lately, so I accidently used 'then' instead of
the Matlab
> equivilent structure:
>
> if some_variable == 0, error('Opps0'), end
> or
> if some_variable == 0
> error('Opps0')
> end
>
> --
> Programming is what happens while you're busy making
other plans.
thank you very much my dear,
I very appreciate your help.
Really I need that. I tried execute your code:

 s=0;
 try for K =1:20

    s=s+1;
 if K == 20
    error('Opps0')
  end
  
 %do_something_more;
 g=10;
 for i=1:10
     s=s+g;
 
end
end
 return
catch
  if lasterror =='Opps0'

  return
  
 rethrow(lasterror)
end
end
but still there is aproblem .there are errors such:
??? Error using ==> ==
Function '==' is not defined for values of class 'struct'.

Error in ==> e:\MATLAB6p5\work\us1.m
On line 18 ==> if lasterror =='Opps0'
thanks again


Subject: Re: go to statment in matlab

From: Peter Boettcher

Date: 21 Aug, 2007 15:05:02

Message: 11 of 14

"huda nawaf" <halmamory@yahoo.com> writes:

> s=0;
> try for K =1:20
>
> s=s+1;
> if K == 20
> error('Opps0')
> end
>
> %do_something_more;
> g=10;
> for i=1:10
> s=s+g;
>
> end
> end
> return
> catch
> if lasterror =='Opps0'
>
> return
>
> rethrow(lasterror)
> end
> end
> but still there is aproblem .there are errors such:
> ??? Error using ==> ==
> Function '==' is not defined for values of class 'struct'.
>
> Error in ==> e:\MATLAB6p5\work\us1.m
> On line 18 ==> if lasterror =='Opps0'
> thanks again
>
>

I don't understand why this needs try/catch. How about this instead:


s=0;
for K =1:20
    s=s+1;
    if K == 20
        break; % exit out of loop
    end

    %do_something_more;
    g=10;
    for i=1:10
        s=s+g;
    end
end


If you need to execute a cleanup condition, use an additional flag,
like

if K == 20
  this_was_a_messy_exit = 1;
  break;
end


-Peter

Subject: Re: go to statment in matlab

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 21 Aug, 2007 18:58:30

Message: 12 of 14

In article <fae87n$651$1@fred.mathworks.com>,
huda nawaf <halmamory@yahoo.com> wrote:

> s=0;
> try for K =1:20
>
> s=s+1;
> if K == 20
> error('Opps0')
> end
>
> %do_something_more;
> g=10;
> for i=1:10
> s=s+g;
>
>end
>end
> return
>catch
> if lasterror =='Opps0'

if strcmp(lasterror.message,'Opps0')

>
> return
>
> rethrow(lasterror)
>end
>end


In my opinion, if you don't have enough programming skills to be able
to debug a minor problem such as that, then you absolutely
positively should not be using try/catch error processing.

I've been programming for 30 years. I think the last time that
I needed a GOTO construct was about 18 years ago, cleaning up
some Fortran code that had been under development by random students
for 20 years.
--
   "No one has the right to destroy another person's belief by
   demanding empirical evidence." -- Ann Landers

Subject: Re: go to statment in matlab

From: James

Date: 28 Feb, 2008 16:35:08

Message: 13 of 14

> I've been programming for 30 years. I think the last time
that
> I needed a GOTO construct was about 18 years ago,
cleaning up
> some Fortran code that had been under development by
random students
> for 20 years.

I know this is old, but I'm eagerly looking forwards to the
addition of GOTO to MatLab.

One simple reason: It's fast. Really fast. I work with
people who do real-time data collection using MatLab and
need to iterate through loops in the range of milliseconds
or less. GOTO's can really help when you need to pare your
code down to nothing!

Subject: Re: go to statment in matlab

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 28 Feb, 2008 17:48:05

Message: 14 of 14

In article <fq6nrs$kq0$1@fred.mathworks.com>,
James <kilrogg311@yahoo.com> wrote:
>> I've been programming for 30 years. I think the last time
>that
>> I needed a GOTO construct was about 18 years ago,
>cleaning up
>> some Fortran code that had been under development by
>random students
>> for 20 years.

>I know this is old, but I'm eagerly looking forwards to the
>addition of GOTO to MatLab.

>One simple reason: It's fast. Really fast. I work with
>people who do real-time data collection using MatLab and
>need to iterate through loops in the range of milliseconds
>or less. GOTO's can really help when you need to pare your
>code down to nothing!

a GOTO in MATLAB would not necessarily be very fast: it might have
to visit all of the statements in-between. You can see this effect
if you profile an if/else construct: even when the else clause
is never taken, the profiler will ascribe time to it.
--
  "Is there any thing whereof it may be said, See, this is new? It hath
  been already of old time, which was before us." -- Ecclesiastes

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
humor someone 09 Aug, 2007 14:15:08
syntax us 09 Aug, 2007 08:30:26
goto us 09 Aug, 2007 08:30:26
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics