emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* matlab+org+write code executing in the buffer, matlab-shell
@ 2017-10-26 16:01 Uwe Brauer
  0 siblings, 0 replies; only message in thread
From: Uwe Brauer @ 2017-10-26 16:01 UTC (permalink / raw)
  To: emacs-orgmode



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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2017-10-26 16:02 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-26 16:01 matlab+org+write code executing in the buffer, matlab-shell Uwe Brauer

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).