emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Tak Kunihiro <tkk@misasa.okayama-u.ac.jp>
To: emacs-orgmode@gnu.org
Cc: tkk@misasa.okayama-u.ac.jp
Subject: orgtbl-to-matlab
Date: Thu, 23 Oct 2014 22:47:24 +0900 (JST)	[thread overview]
Message-ID: <20141023.224724.49651895.tkk@misasa.okayama-u.ac.jp> (raw)

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

                 reply	other threads:[~2014-10-23 13:47 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=20141023.224724.49651895.tkk@misasa.okayama-u.ac.jp \
    --to=tkk@misasa.okayama-u.ac.jp \
    --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).