2

I want to create a re-usable matlab org-babel source block vectsb (say), which takes two vectors from a call statement, performs some operations with them, and returns the result in the call statement. A MWE is as follows

#+NAME: vectsb
#+begin_src matlab :var a=[0], b=[0] :session 
a+b
#+end_src

#+CALL: vectsb(a=[1,2,3],b=[3,2,1])

#+RESULTS:
| 1040 | 650 |

I am getting this type of errors in the input arguments

a=[1 (, 2) (, 3)];
       |
Error: Invalid expression. When calling a function or indexing a variable, use
parentheses. Otherwise, check for mismatched delimiters.

No matter how much I read the org-babel manual I can't figure out what I am doing wrong. Partly because there are few examples for Matlab.

Ajned
  • 672
  • 4
  • 19

1 Answers1

1

Here's a version that seems to work (discovered by trial-and-error; and BTW it is using octave, since I don't have matlab):

  #+NAME: vectsb
  #+begin_src octave :var a=[0] :var b=[0] :session
    a+b
  #+end_src

  #+RESULTS: vectsb
  : 0
  
  #+CALL: vectsb(a=[1 2 3],b=[3 2 1])

  #+RESULTS:
  | 4 | 4 | 4 |

The org-babel section of the Org mode manual contains very little about individual languages, which are decribed in a bit more detail in Worg. Unfortunately, the octave/matlab page is very rudimentary: if you are going to work with octave/matlab, you might want to request edit rights to Worg and improve it.

NickD
  • 27,023
  • 3
  • 23
  • 42
  • Unbelievable. To hit a wall because of not adding `:var` for each input argument and using spaces instead of commas when passing on the vectors in the call statement. Thank you very much! Org-mode addictiveness has just increased another thousandfold. – Ajned Oct 25 '20 at 18:52
  • The separate `:var` header argument for each variable is required by Org mode and you will just have to get used to it. However, `octave` (and I presume `matlab`) syntax requires commas when specifying a literal vector, so it it natural to include them: why Org mode objects to that is not clear to me - I assume that it has something to do with the (generic) parsing of `#+CALL:` arguments, but I have not checked the code. But I'm glad you are past that road block - and still feel positively towards Org mode! – NickD Oct 25 '20 at 20:27
  • Strike that: octave (and matlab?) do *NOT* require commas when specifying a literal vector - you *can* use spaces: `[1 2 3]` works. So Org mode does not support the commas, but it does support the spaces notation. That's much better than I thought. – NickD Oct 25 '20 at 22:44