Error when running simple script containing local function in Octave

前端 未结 2 1732
闹比i
闹比i 2020-12-18 09:48

My file testtest.m looks like :

pluse(1, 2)
function retval = pluse(input1, input2)
retval = input1 + input2;
endfunction

Then I get:

相关标签:
2条回答
  • 2020-12-18 10:32

    To answer your question properly, I need to point out two things:

    1. The canonical way to create a function in both octave and matlab is by placing it in a dedicated file by the same name, and starting the file with a function declaration. The corollary of this is that any file in the path that starts with a function declaration is detected at start-up and made available to the user as a callable function.

    2. Octave has supported 'on-the-go' function definitions for many years (and in fact also supports subsequent 'exporting' of such 'on-the-go' functions to files); matlab has only included 'on-the-go' functionality very recently (2016b I believe?), and has chosen to implement this somewhat differently than octave, ( presumably to keep octave on its toes? :p )

    Octave's implementation effectively follows straightforwardly from the rules of the language. Any file that starts with a statement that isn't a 'function declaration' is treated as a script, i.e. a sequence of independent statements. Hence the innocuous 1 at the start of your script which is as simple a 'non-function-declaration' statement as it gets, but really it could be anything. A script can then have as many 'on-the-go' function definitions as desired. For a statement to make use of a function in an 'on-the-go' context, the function clearly needs to have been defined first. This follows from the fundamental principle that a script, in contrast to a function, effectively represents a simple collection of statements that are run in sequence without any pre-processing, and that one could expect to simply copy/paste these commands to or from their terminal and expect them to run.

    Matlab's recent implementation effectively breaks this functionality / paradigm. To see what I mean, copy paste your code above into a fresh terminal (or highlight then press F9) and watch it break. A script is no longer copy/pasteable, but assumes matlab will read ahead and load up any function definitions first, and then go back and try to run the remaining commands; in other words, matlab now kinda treats scripts like it does its functions. Note that matlab still also requires a script to start with a non-function-declaration statement, so effectively this bit is the same as octave needing to start with a '1'. The only thing that's changed is this look-ahead behaviour for preemptively loading 'on-the-go' functions, which I would argue isn't necessarily a good thing in the context of scripts.

    I would argue that Octave's approach makes more sense, despite the convenience you point out with matlab when (unintentionally) treating a script as effectively not a script. Which one presumably shouldn't do in the first place. If you need look-ahead functionality and nesting, you really should be writing a proper function and providing relevant scoping context in the first place.

    0 讨论(0)
  • 2020-12-18 10:37

    I had the same question/problem and some people gave the hints. But since there is no explicit example, I post it here so that other guys can find a explict running example both for Octave and MATLAB.

    % works in Octave %%%
    % sth. must be *before* a (local) function is declared
    1; % or "2;" or "1+1;" or whatever
    
    % local function must be declared *before* it is run in Octave
    function retval = pluse(input1, input2)
      retval = input1 + input2;
    end % or "endfunction"
    
    % Now you can use the local function
    pluse(1, 2)
    

    And there the the incompatibility between Octave and MATLAB because the MATLAB example does not run in Octave and vice versa:

    % works in MATLAB %%%
    % You can use the local function
    pluse(1, 2)
    
    % local function must be declared at the end of file for MATLAB
    function retval = pluse(input1, input2)
      retval = input1 + input2;
    end
    

    Because of this incompatibility the question is if one should really use local functions. Maybe one should use "normal" functions in a file...

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