From mboxrd@z Thu Jan 1 00:00:00 1970 From: Uwe Brauer Subject: matlab+org+write code executing in the buffer, matlab-shell Date: Thu, 26 Oct 2017 18:01:33 +0200 Message-ID: <87bmkt6gpu.fsf@mat.ucm.es> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:48471) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e7kbX-00016I-Oe for emacs-orgmode@gnu.org; Thu, 26 Oct 2017 12:02:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e7kbS-0008Ur-4P for emacs-orgmode@gnu.org; Thu, 26 Oct 2017 12:01:59 -0400 Received: from [195.159.176.226] (port=43177 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1e7kbR-0008Ty-UF for emacs-orgmode@gnu.org; Thu, 26 Oct 2017 12:01:54 -0400 Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1e7kbF-0008SF-Pp for emacs-orgmode@gnu.org; Thu, 26 Oct 2017 18:01:41 +0200 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org 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