emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* orgtbl-to-matlab
@ 2014-10-23 13:47 Tak Kunihiro
  0 siblings, 0 replies; only message in thread
From: Tak Kunihiro @ 2014-10-23 13:47 UTC (permalink / raw)
  To: emacs-orgmode; +Cc: tkk

Since LaTeX + radiotable is so comfortable, I start to use it on
MATLAB coding.  The translator may be useful for some and I want to
let you know.

%{
#+ORGTBL: SEND tbl:radiotable orgtbl-to-matlab :no-escape t
|    x |     y | symbol |
|------+-------+--------|
| 0.79 | 0.243 | +      |
| 0.78 | 0.230 | x      |
| 0.78 | 0.242 | .      |
%}
% BEGIN RECEIVE ORGTBL tbl:radiotable
x = [0.79 0.78 0.78];
y = [0.243 0.230 0.242];
symbol = {'+','x','.'};
% END RECEIVE ORGTBL tbl:radiotable


(defun orgtbl-to-matlab (table params)
  "Convert the orgtbl-mode table to MATLAB statements."
  (let* ((*list-is-num* org-table-last-alignment)
         (*table-flip* (orgtbl-to-orgtbl-flip table))
         (*index* (number-sequence 0 (1- (length *table-flip*)))))
    (mapconcat (lambda (ii) (orgtbl-to-matlab--column
                             (nth ii *table-flip*) params (nth ii *list-is-num*)))
               *index* "\n")))

(defun orgtbl-to-matlab--column (table-nth-col params is-num-p)
  "Convert a column of the orgtbl-mode table to a MATLAB statement."
  (let ((params2 (list
                   :lstart (if is-num-p "[" "{'")
                   :sep    (if is-num-p " " "','")
                   :lend   (if is-num-p "];" "'};")
                   :no-escape t))
         (*var-name* (car table-nth-col))
         (*dataset* (cdr table-nth-col)))
    (if is-num-p ;; return NaN when a cell in orgtbl is empty
        (setq *dataset* (mapcar
                         (lambda (x) (if (string= x "") "NaN" x)) *dataset*)))
    (format "%s = %s"
            *var-name*
            (orgtbl-to-generic (list *dataset*) (org-combine-plists params2 params)))))

(defun orgtbl-to-orgtbl-flip (table)
  "Flip the orgtbl-mode table."
  (with-temp-buffer
    (insert (orgtbl-to-orgtbl table nil))
    (goto-char (1+ (org-table-begin)))
    (org-table-transpose-table-at-point)
    (goto-char (1+ (org-table-begin)))
    (org-table-to-lisp)))

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2014-10-23 13:47 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-23 13:47 orgtbl-to-matlab Tak Kunihiro

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