Hi

Org mode is a mayor mode which becomes increasing popular since it ships
with an amazing amount of features.

One of them is to execute code and insert the result in the org buffer,
formatted if wished.
Now matlab is not really supported natively, but John Kitchin posted a
while ago a nice solution on the org mailing list.

(defun org-babel-execute:matlab-john-org (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 <"
;                    "-nodesktop -nojvm -nosplash -nodisplay <"
;                    "-nodesktop -nojvm -nosplash  <"
                     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))))

There is a problem with his code, however: It starts every time it is
called the whole matlab engine, making it quite slow.

So I thought of running the emacs matlab shell running and use it
instead. So I came up with the following

(defun org-babel-execute:matlab-john-shell (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))))
    (let ((results
           (matlab-shell-collect-command-output code)))
      (org-babel-result-cond result-params
        results))))



That works, however take the following example
#+begin_src matlab :results output latex :exports results :wrap latex
X=[1,2,3,4,5,6,7];
p=[1/7 1/7 1/7 1/7 1/7 1/7 1/7];
E=X*p';
x2=X.*X;
E2=x2*p';
V=E2-E^2;
disp('\begin{align}')
fprintf('E[X]&=%g\\\\ \n', E)
fprintf('E^2[X]&=%g\\\\\n', E2)
fprintf('V[X]&=%g\n', V)
disp('\end{align}')
disp('\end{align}')
#+end_src

By code only extracts:
#+RESULTS:
#+BEGIN_latex
>> p=[1/7 1/7 1/7 1/7 1/7 1/7 1/7];
>> E=X*p';
>> x2=X.*X;
\begin{align}
E[X]&=4\\ 
E^2[X]&=20\\
V[X]&=4
#+END_latex


But not the last 2 lines of the original matlab code.

Anybody with a better  know of the matlab-shell can explain that to me
please? In order to test the code I should send a bunch of more
functions, which I attach for convince.


Regards

Uwe Brauer




Attachment: matlab-org-init.el
Description: application/emacs-lisp

Attachment: smime.p7s
Description: S/MIME cryptographic signature

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Matlab-emacs-discuss mailing list
Matlab-emacs-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matlab-emacs-discuss

Reply via email to