I tried to define and use a simple matlab/octave function within an org-mode code block as shown follows.

#+name: test-octave-function
#+begin_src octave
  function s = my_sum(x, y)
  s = x + y;

  my_sum(1, 2)

The code works if put inside a .m file. However, in a code-block, it generates this error in Org-babel output:

warning: function name 'my_sum' does not agree with function filename ''

error: 'my_sum' undefined near line 1 column 1

I understand functions defined for use in other files need to have the same name as the .m file. But how do filenames work for a code block. Also, I called the function my_sum in the same code block, why is it undefined?

Related to this, how can I define and use multiple functions in a code block?

(This is with the stock Emacs 26 of Ubuntu 20.04)

  • Please try to ask only one question per question. You've asked at least 3 here. Consider posting separate questions.
    – Drew
    Jun 2, 2020 at 22:36

1 Answer 1


I get the same result as you do and I'm not sure how it is supposed to work (I suspect bugs but I have not investigated).

However, it does work better in a session:

#+name: test-octave-function
#+begin_src octave :session foo
function retval = mysum(x, y)
  retval = x + y;

ans = mysum(5, 12)

#+RESULTS: test-octave-function
: 17
  • Thanks. I just tried this, but the No org-babel-execute function for octave issue (emacs.stackexchange.com/questions/58856) came back after adding :session. load-library on ob-octave doesn't fix it this time. After removing :session, the code block can be evaluated, but the function undefined problem here came back.
    – tinlyx
    Jun 3, 2020 at 10:24
  • I removed my elpa folder, and re-initiated all packages, and now it seems to work (so far).
    – tinlyx
    Jun 3, 2020 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.