maroxe
maroxe

Reputation: 2127

How to define matlab function in the same file in org mode

I am using org-mode to write reports for my school. It is very convenient because I can include some Matlab code in the same file. The problem however, is that I cannot define new functions as Matlab require functions to be declared in a separate file.

This is very inconvenient because it prevents me from having only one central file to modify.

Upvotes: 2

Views: 391

Answers (1)

John Kitchin
John Kitchin

Reputation: 2433

This works for me using nested functions. The output is kind of ugly, but there is no file creation here, just a code block that is sent to Matlab. (This works on MacOSX and Linux. It used to not work on Windows, not sure about about now).

#+BEGIN_SRC matlab
function parent
disp('This is the parent function')
nestedfx

   function nestedfx
      disp('This is the nested function')
   end

end
#+END_SRC

#+RESULTS:
#+begin_example

                            < M A T L A B (R) >
                  Copyright 1984-2013 The MathWorks, Inc.
                     R2013a (8.1.0.604) 64-bit (maci64)
                             February 15, 2013


To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.

>> >> This is the parent function
>> >> >> >> This is the nested function
>> >> >> >> 
#+end_example

To use external functions, you will have use tangle, e.g. First, define your function.

#+BEGIN_SRC matlab :tangle myfunc.m
function myfunc
    disp('External function')
end
#+END_SRC

this next block tangles the m-file out.

#+BEGIN_SRC emacs-lisp
(org-babel-tangle)
#+END_SRC
#+RESULTS:
| myfunc.m |

Now we use it in a code block

#+BEGIN_SRC matlab
myfunc()
#+END_SRC

#+RESULTS:
#+begin_example

                            < M A T L A B (R) >
                  Copyright 1984-2013 The MathWorks, Inc.
                     R2013a (8.1.0.604) 64-bit (maci64)
                             February 15, 2013


To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.

>> External function
>> 
#+end_example

Upvotes: 2

Related Questions