|
From: Uwe B. <ou...@ma...> - 2017-10-24 21:20:12
|
Hi
I am using more and more org mode, a long with auctex and matlab.el.
Orgmode comes in handy if I write a document which contains (matlab)
code which I want to execute, but the rest of the file maybe text, which
I can then export to latex.
So a typical file looks like
Given
\droppoints
\begin{displaymath}
\begin{cases}
\frac{du}{dt}&= -t u^2,\\
u(0)&=1,
\end{cases}
\end{displaymath}
Solve using ...
#+BEGIN_solution
#+begin_src matlab :results output latex :exports results :eval never-export
f=@(t,y)[-t*y^2];
[t,y]=mieuler(f,[0 1],1,2);
disp('\begin{align*}')
fprintf('t = [%g \\qquad %g \\quad %g] \\\\\n', t)
fprintf('y = [%g \\qquad %g \\qquad %g] \n', y)
disp('\end{align*}')
#+end_src
Now I can execute the matlab code in that org file using
C-c C-c ( org-ctrl-c-ctrl-c, the *one-ring-to-rule-them-all* function in
orgmode.
Thanks thanks to a function provided to me by John Kitchin it executes
matlab.
It is as follows:
(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))))
The problem of that code is that it starts matlab every time I execute
C-c C-c . There seems to be a python based kernel around which avoids
this, but I thought that maybe with a bit of hacking one could use the
elisp based matlab-shell and the corresponding functions.
Anybody has an idea, Eric?
Needless to say that I played a bit around but did get it to work....
Regards
Uwe Brauer
|