emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Uwe Brauer <oub@mat.ucm.es>
To: emacs-orgmode@gnu.org
Subject: matlab+org+write code executing in the buffer, matlab-shell
Date: Thu, 26 Oct 2017 18:01:33 +0200	[thread overview]
Message-ID: <87bmkt6gpu.fsf@mat.ucm.es> (raw)



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

                 reply	other threads:[~2017-10-26 16:02 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bmkt6gpu.fsf@mat.ucm.es \
    --to=oub@mat.ucm.es \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).