Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a single Octave script with function definitions compatible with Matlab scripts?

If I write this:

clc
clear

close all
format long
fprintf( 1, 'Starting...\n' )

function results = do_thing()
    results = 1;
end

results = do_thing()

And run it with Octave, it works correctly:

Starting...
results =  1

But if I try to run it with Matlab 2017b, it throws this error:

Error: File: testfile.m Line: 13 Column: 1
Function definitions in a script must appear at the end of the file.
Move all statements after the "do_thing" function definition to before the first local function
definition.

Then, if I fix the error as follows:

clc
clear

close all
format long
fprintf( 1, 'Starting...\n' )

results = do_thing()

function results = do_thing()
    results = 1;
end

It works correctly on Matlab:

Starting...

results =

     1

But now, it stopped working with Octave:

Starting...
error: 'do_thing' undefined near line 8 column 11
error: called from
    testfile at line 8 column 9

This problem was explained on this question: Run octave script file containing a function definition

How to fix it without having to create a separate and exclusive file for the function do_thing()?

Is this issue fixed on some newer version of Matlab as 2019a?

like image 362
user Avatar asked Oct 25 '25 02:10

user


1 Answers

The answer is in the comments, but for the sake of clarity:

% in file `do_thing.m`
function results = do_thing()
    results = 1;
end

% in your script file
clc;   clear;   close all;   format long;
fprintf( 1, 'Starting...\n' );
results = do_thing();

Accompanying explanatory rant:

  • The canonical and safest way to define functions is to define them in their own file, and make this file accessible in octave / matlab's path.
  • Octave has supported 'dynamic' function definitions (i.e. in the context of a script or the command-line) since practically forever. However, for the purposes of compatibility, since matlab did not support this, most people did not use it, and quite sensibly relied on the canonical way instead.
  • Matlab has recently finally introduced dynamic function definitions too, but has opted to implement them explicitly in a way that breaks compatibility with octave, as you describe above. (rant: this may be a coincidence and an earnest design decision, but I do note that it also happens to go against prior matlab conventions regarding nested functions, which were allowed to be defined anywhere within their enclosing scope).
  • In a sense, nothing has changed. Matlab was incompatible with advanced octave functionality, and now that it has introduced its own implementation of this functionality, it is still incompatible. This is a blessing in disguise. Why? Because, if you want intercompatible code, you should rely on the canonical form and good programming practices instead of littering your scripts with dynamic functions, which is what you should be doing anyway.
like image 81
Tasos Papastylianou Avatar answered Oct 26 '25 21:10

Tasos Papastylianou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!