Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Fri, 15 Dec 2006 11:59:07 -0600
From: Harmonic Software <info@omatrix.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to do GOTO in Matlab
Date: Fri, 15 Dec 2006 10:59:09 -0700
Message-ID: <MPG.1fec9055fb42308d9896a1@news.giganews.com>
References: <1165778984.266098.310630@73g2000cwn.googlegroups.com> <ef4839d.0@webcrossing.raydaftYaTP>
Organization: Harmonic
X-Newsreader: MicroPlanet Gravity v2.50
Lines: 58
X-Trace: sv3-6PD6eAnBwy0o+/JbdleP60uyodtKZG9JSdR+kIMaqGqTcVzKHLWX3cjAzXUD7JJsQ1vkCM+EDOf7O+M!SC2NKmW9D22lIGgPEX96UnfsokGEJ9/txf5+o0q9UR/yft3DpQJAerLPkvHJGaI20EPwkADaBFM=
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.32
Xref: news.mathworks.com comp.soft-sys.matlab:384034



[This followup was posted to comp.soft-sys.matlab and a copy was sent to 
the cited author.]

We implemented labeled gotos in O-Matrix, 
http://www.omatrix.com/manual/goto.htm and although this construct needs 
to be used judicously it has proven very popular.

In article <ef4839d.0@webcrossing.raydaftYaTP>, poi1@kth2.se says...
> daniel_nordlund_1982 wrote:
> >
> >
> > I've seen many people on this newsgroup ask for a GOTO statement.
> > Since
> > such a statement doesn't exist, I've decided to help MathWorks
> > implement one. Rather than a line number approach, I've based it on
> > named labels. Here's a fine example of how you can use it:
> >
> > s=input('Are you sure (Y)es, (N)o, (M)aybe? ','s');
> > goto lower(s(1));
> > m:: goto 'n'+(rand>.5)*11;
> > y:: fprintf('You are sure\n'), goto 'ok';
> > n:: fprintf('You are not sure\n')
> > ok::
> >
> > Note that labels end with :: and goto statements end with a ;. The
> > expression in a goto statement is converted to a string and
> > execution
> > continues at the corresponding label. Illegal label names simply
> > terminate the program.
> >
> > To be able to run this code, you must first transform it to valid
> > Matlab code. Just use the following helper function:
> >
> > function s=gotofix(g)
> > f=fopen(g,'rt'); s=char(fread(f)'); fclose(f);
> > s=sprintf(['IP=pi; while IP try switch IP case pi\n%s IP=[];' ...
> > ' otherwise,IP=[];\nend,catch,[EM IP]=lasterr;if nnz(IP)<2|' ...
> > 'IP(1)*IP(2)-6554\nerror(IP,EM),end,IP(1:2)=[];end,end\n'], ...
> > regexprep(s,{'(?m)^(.)','([\s,;])goto\s*([^;]+)','(\w+)\s*::' ...
> > },{' $1','$1error([''q:'' $2],'''')','IP=''$1''; case''$1'','}));
> >
> > Send the input m-file as an argument and the transformed file will
> > be
> > returned. The result can be stored to a new m-file or executed
> > directly:
> >
> >>> eval(gotofix('areyousure.m'))
> >
> > For those interested, here's what the transformed version of the
> > example will look like:
> >
> > IP=pi; while IP try switch IP case pi
> > s=input('Are you sure (Y)es, (N)o, (M)aybe? ','s');
> > error(['q:' lower(s(1))],'');
> > IP='m'; case'm', error(['q:' 'n'+(rand>.5)*11],'');
> > IP='y'; case'y', fprintf('You are sure\n'), error('q:ok','');
> > IP='n'; case'n', fprintf('You are not sure\n')
> > IP='ok'; case'ok',