Hi Uwe,

I did see this on the list the first time, and I spent a little time on it.  I 
was surprised to see the result from using matlab-shell-collect-command-output 
cropping as you suggested.  There are a few filters for things like errors, 
anchors, and other misc text to hide that I played with, but I found nothing 
that matched \end at that time so I didn't end up replying as I hadn't found 
anything.  There may be something else with the special MATLAB end keyword I 
missed.  Process filters are a challenge to debug, particularly when there is 
both the matlab filter plus the gud filter interacting which I'm just not that 
familiar with anymore.

If you start in gud-matlab-marker-filter, you can see some of the stuff it 
filters, such as escape codes, error, opentoline, etc.   Anyway, I didn't see 
anything there.

matlab-shell-collect-command-output perhaps could cheat, and disable the 
process filter near the loop that accepts process output.   For example:

(condition-case nil
     (set-process-filter (get-buffer-process (current-buffer))
                              nil)
     ;; loop is here
  (error nil))
(set-process-filter (get-buffer-process (current-buffer))
                              'gud-filter)

but that's a pretty big hammer.  I guess it would tell us if the filter is the 
problem.   If it is in this loop, there may be an issue in prompt detection.  
The bit that cuts out the string to return is pretty simple.  Perhaps it is 
being confused by newlines or something.

Eric


-----Original Message-----
From: Uwe Brauer [mailto:o...@mat.ucm.es] 
Sent: Monday, July 02, 2018 4:45 AM
To: matlab-emacs-discuss <matlab-emacs-discuss@lists.sourceforge.net>
Cc: John Ciolfi <john.cio...@mathworks.com>; Eric Ludlam <ericlud...@gmail.com>
Subject: [Matlab-emacs-discuss] org mode: execute matlab code and insert it, 
the matlab shell, matlab live editor, jupiter notebook


Hi

I am not sure whether this mail made to the list, I never saw it. I also cc 
this message to John and Eric since they might know more about the matlab shell.

One of new features in matlab is the matlab live editor, which allows to mix 
documentation, matlab code and, most importantly, the results of executed 
matlab code. 

It seems that the jupiter notebooks should have a similar feature, but I have 
not tried it out.

The question is whether there is a similar solution using emacs. 

And the answer to this is yes, but it is, at the moment, quite slow, at least 
with recent matlab releases.

The point is to use org mode.

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





------------------------------------------------------------------------------
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