Path: news.mathworks.com!not-for-mail
From: "Steven Lord" <slord@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to do GOTO in Matlab
Date: Fri, 15 Dec 2006 14:27:26 -0500
Organization: The MathWorks, Inc.
Lines: 104
Message-ID: <elusuu$5gd$1@fred.mathworks.com>
References: <1165778984.266098.310630@73g2000cwn.googlegroups.com>
Reply-To: "Steven Lord" <slord@mathworks.com>
NNTP-Posting-Host: lords.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1166210846 5645 144.212.105.187 (15 Dec 2006 19:27:26 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 15 Dec 2006 19:27:26 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2869
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2962
Xref: news.mathworks.com comp.soft-sys.matlab:384041




<daniel_nordlund_1982@hotmail.com> wrote in message 
news:1165778984.266098.310630@73g2000cwn.googlegroups.com...
> 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::

I would probably implement the above example with a SWITCH statement:


function yn = areyousure
yn = [];
while isempty(yn)
    s = input('Are you sure?  (Y)es, (N)o, or (M)aybe:', 's');
    if ~isempty(s)
        switch lower(s(1))
        case 'y'
            yn = true;
        case 'n'
            yn = false;
        case 'm'
            disp('Please decide if you are sure or if you are not.');
        otherwise
            disp('I''m sorry, I didn''t understand your answer.');
        end
    else
        disp('Please choose an answer.');
    end
end


or, if I'm allowed to display a dialog box, like this:


function yn = areyousure2
yn = [];
while isempty(yn)
    ButtonName = questdlg('Are you sure?', 'Confirmation dialog', ...
        'Yes', 'No', 'Maybe', 'Yes');
    switch ButtonName
        case 'Yes'
            yn = true;
        case 'No'
            yn = false;
        case 'Maybe'
            disp('Please decide if you are sure or if you are not.');
    end
end


> 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',
>  IP=[]; otherwise,IP=[];
> end,catch,[EM IP]=lasterr;if nnz(IP)<2|IP(1)*IP(2)-6554
> error(IP,EM),end,IP(1:2)=[];end,end

In my personal opinion, that code looks much more confusing than the 
SWITCH-CASE code.  I think it would take me a few minutes of thought to 
figure out what it's doing, especially the last two lines, if I had to go 
back to it a week, a month, or a year from now.

-- 
Steve Lord
slord@mathworks.com