Can a Matlab M-script be stopped by a statement in the script?

后端 未结 3 492
误落风尘
误落风尘 2021-02-05 06:17

A very simple, and perhaps obvious, question: How can I abort execution of a Matlab M-script using a statement within the script?

This is analogous to calling ret

相关标签:
3条回答
  • 2021-02-05 06:41

    Yes you can with the help of

    return;

    Return works in Matlab-scripts like it does in functions.

    e.g.

        function [ point ] = PointDoubling( x,y,p,a )
        %UNTITLED2 Summary of this function goes here
        %   Detailed explanation goes here
        if y==0
            point='Not calculated';
            return;
        end
        a2=(3*(x^2))+a;
        b2=(2*y);
        i=1;
        while 1
            if mod(b2*i,p)==1
            break;
        end
            i=i+1;
        end
        s=mod(a2*i,p);
        x1=mod(((s^2)-(2*x)),p);
        y1=mod(((-y)+(s*(x-x1))),p);
        point=[x1,y1];
        end
    
    0 讨论(0)
  • 2021-02-05 06:43

    If return is not want you need, I think you want to use break

    break terminates the execution of a Matlab code. For example, statements in the loop that appear after the break statement are not executed.

    In nested loops, break exits only from the loop in which it occurs. Control passes to the statement that follows the end of that loop.

    0 讨论(0)
  • 2021-02-05 06:51

    As of Matlab R2015b break can no longer be used to pre-terminate a script. A break can now only be used to a for-loop. The code will not run and an error will be thrown. This was technically always true, but it is now enforced.

    The proper way is to use return

    0 讨论(0)
提交回复
热议问题