[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] matlab+org+write code executing in the buffer, matlab-shell

From: Uwe Brauer
Subject: [O] matlab+org+write code executing in the buffer, matlab-shell
Date: Thu, 26 Oct 2017 18:01:33 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux)


Actually I am using two different approaches to execute matlab code in a
orgmode buffer.

* Chumur Erkut suggestion:

Don't change the definition of 

 but do

(setq org-babel-default-header-args:matlab
  '((:session . "*MATLAB*")
    (:results . "silent")    ))

This way when executing matlab, the matlab-shell written in lisp is
used, and matlab only has to be started once.

Here is his suggestion

#+BEGIN_SRC emacs-lisp

 '((latex . t)
   (plantuml . t)
   (java . t)
   (matlab . t)
   (C . t)
   (table . t)  ))

(defun org-babel-execute:matlab (body params)
  "Execute a block of matlab code with Babel."
  (org-babel-execute:octave body params 'matlab))

(setq org-babel-default-header-args:matlab
  '((:session . "*MATLAB*")
    (:results . "silent")    ))


: ((:session . *MATLAB*) (:results . silent))

So executing 

#+BEGIN_SRC matlab :results output org drawer
x = [1, 2, 3, 4, 5];
fprintf('|%d', x)

does not result in code results to be inserted.

* John Kitchin's code

However I also need to insert the result of executed code into my org
buffer, best without the matlab >.

John Kitchin provided me the following rewrite of
in order that his code works I need 
org-babel-default-header-args:matlab to set to nil.

Here is his code

#+BEGIN_SRC emacs-lisp

(setq org-babel-default-header-args:matlab nil)

 (defun org-babel-execute:matlab (body params) 
  (interactive "P")
  (let* ((current-file (buffer-file-name)) 
         (code (org-element-property :value (org-element-context)))
         (result-params (cdr (assoc :result-params params)))
      (insert code)
      (setq md5-hash (md5 (buffer-string))
            mbuffer (format "*m-%s*" md5-hash)
            m-file (format "m-%s.m" md5-hash)))
    ;; create the file to run
    (with-temp-file m-file 
      (insert code))
    (let ((results (shell-command-to-string
                     "/usr/local/bin/matlab "
                     "-nodesktop -nojvm -nosplash -nodisplay <"
      (delete-file m-file)
      (when results
        ;; strip out >>
        (setq results (replace-regexp-in-string ">> " "" results))
        ;; remove first 10 lines that are the header.
        ;; matlab license seem to expire soon, so 5 warning lines are added
        ;; change first 10 to first 15 lines
        (setq results (mapconcat 'identity
                                 (nthcdr 10 (split-string results "\n"))
      (org-babel-result-cond result-params


#+BEGIN_SRC matlab :results output org drawer
x = [1, 2, 3, 4, 5];
fprintf('|%d', x)

Results in 
| 1 | 2 | 3 | 4 | 5 |

Which is very nice. The only problem, every time the code is executed
matlab is restarted, which in recent version of matlab can take some

Does somebody has an idea how to combine both code, take John's code but
use the lisp matlab shell???

(John recommended to use the python kernel, but at least in Ubunutu
14.04 32 bit Matlab2012A does not provide that kernel) and in Ubuntu
16.06 with Matlab 2014 it also seems not to work.


Uwe Brauer

reply via email to

[Prev in Thread] Current Thread [Next in Thread]