[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) |
Hi
Actually I am using two different approaches to execute matlab code in a
orgmode buffer.
* Chumur Erkut suggestion:
Don't change the definition of
org-babel-execute:matlab
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
(org-babel-do-load-languages
'org-babel-load-languages
'((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") ))
#+END_SRC
#+RESULTS:
: ((:session . *MATLAB*) (:results . silent))
So executing
#+BEGIN_SRC matlab :results output org drawer
x = [1, 2, 3, 4, 5];
fprintf('|%d', x)
#+END_SRC
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
org-babel-execute:matlab
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)))
m-file
md5-hash)
(with-temp-buffer
(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
(concat
"/usr/local/bin/matlab "
"-nodesktop -nojvm -nosplash -nodisplay <"
m-file))))
(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"))
"\n")))
(org-babel-result-cond result-params
results))))
#+END_SRC
Now
#+BEGIN_SRC matlab :results output org drawer
x = [1, 2, 3, 4, 5];
fprintf('|%d', x)
#+END_SRC
Results in
#+RESULTS:
:RESULTS:
| 1 | 2 | 3 | 4 | 5 |
:END:
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
time.
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.
Regards
Uwe Brauer
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [O] matlab+org+write code executing in the buffer, matlab-shell,
Uwe Brauer <=