matlab and org mode example
This commit is contained in:
parent
bda900fd75
commit
b7007223bd
|
@ -0,0 +1 @@
|
|||
*.log
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Used by ../*.org via
|
||||
* #+HTML_HEAD_EXTRA: <link rel="stylesheet" type="text/css" href="css/styles.css"/>
|
||||
*/
|
||||
|
||||
body {
|
||||
font-size: 11pt;
|
||||
text-align: left;
|
||||
line-height:1.2em;
|
||||
}
|
||||
|
||||
h1 {font-size: 14pt;}
|
||||
|
||||
.title {
|
||||
padding-bottom: 7px;
|
||||
margin-bottom: 20px;
|
||||
border-bottom: 1px solid #222;
|
||||
}
|
||||
|
||||
h2 {
|
||||
font-size: 12pt;
|
||||
padding-bottom: 4px;
|
||||
margin-top: 5px;
|
||||
margin-bottom: 5px;
|
||||
border-bottom: 1px solid #DDD;
|
||||
}
|
||||
|
||||
h3 {font-size: 11pt; color: #0000ff;}
|
||||
h4 {font-size: 9pt; color: #a34d32;}
|
||||
|
||||
a {text-decoration: none; color: #537d7b}
|
||||
a:visited {text-decoration: none; color: #98855b}
|
||||
a:hover {text-decoration: underline; color: #a34d32}
|
|
@ -0,0 +1,440 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
|
||||
<head>
|
||||
<!-- 2023-04-10 Mon 15:58 -->
|
||||
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>‎</title>
|
||||
<meta name="generator" content="Org mode" />
|
||||
<meta name="author" content="John Ciolfi" />
|
||||
<style type="text/css">
|
||||
<!--/*--><![CDATA[/*><!--*/
|
||||
.title { text-align: center;
|
||||
margin-bottom: .2em; }
|
||||
.subtitle { text-align: center;
|
||||
font-size: medium;
|
||||
font-weight: bold;
|
||||
margin-top:0; }
|
||||
.todo { font-family: monospace; color: red; }
|
||||
.done { font-family: monospace; color: green; }
|
||||
.priority { font-family: monospace; color: orange; }
|
||||
.tag { background-color: #eee; font-family: monospace;
|
||||
padding: 2px; font-size: 80%; font-weight: normal; }
|
||||
.timestamp { color: #bebebe; }
|
||||
.timestamp-kwd { color: #5f9ea0; }
|
||||
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
|
||||
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
|
||||
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
|
||||
.underline { text-decoration: underline; }
|
||||
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
|
||||
p.verse { margin-left: 3%; }
|
||||
pre {
|
||||
border: 1px solid #ccc;
|
||||
box-shadow: 3px 3px 3px #eee;
|
||||
padding: 8pt;
|
||||
font-family: monospace;
|
||||
overflow: auto;
|
||||
margin: 1.2em;
|
||||
}
|
||||
pre.src {
|
||||
position: relative;
|
||||
overflow: visible;
|
||||
padding-top: 1.2em;
|
||||
}
|
||||
pre.src:before {
|
||||
display: none;
|
||||
position: absolute;
|
||||
background-color: white;
|
||||
top: -10px;
|
||||
right: 10px;
|
||||
padding: 3px;
|
||||
border: 1px solid black;
|
||||
}
|
||||
pre.src:hover:before { display: inline;}
|
||||
/* Languages per Org manual */
|
||||
pre.src-asymptote:before { content: 'Asymptote'; }
|
||||
pre.src-awk:before { content: 'Awk'; }
|
||||
pre.src-C:before { content: 'C'; }
|
||||
/* pre.src-C++ doesn't work in CSS */
|
||||
pre.src-clojure:before { content: 'Clojure'; }
|
||||
pre.src-css:before { content: 'CSS'; }
|
||||
pre.src-D:before { content: 'D'; }
|
||||
pre.src-ditaa:before { content: 'ditaa'; }
|
||||
pre.src-dot:before { content: 'Graphviz'; }
|
||||
pre.src-calc:before { content: 'Emacs Calc'; }
|
||||
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
|
||||
pre.src-fortran:before { content: 'Fortran'; }
|
||||
pre.src-gnuplot:before { content: 'gnuplot'; }
|
||||
pre.src-haskell:before { content: 'Haskell'; }
|
||||
pre.src-hledger:before { content: 'hledger'; }
|
||||
pre.src-java:before { content: 'Java'; }
|
||||
pre.src-js:before { content: 'Javascript'; }
|
||||
pre.src-latex:before { content: 'LaTeX'; }
|
||||
pre.src-ledger:before { content: 'Ledger'; }
|
||||
pre.src-lisp:before { content: 'Lisp'; }
|
||||
pre.src-lilypond:before { content: 'Lilypond'; }
|
||||
pre.src-lua:before { content: 'Lua'; }
|
||||
pre.src-matlab:before { content: 'MATLAB'; }
|
||||
pre.src-mscgen:before { content: 'Mscgen'; }
|
||||
pre.src-ocaml:before { content: 'Objective Caml'; }
|
||||
pre.src-octave:before { content: 'Octave'; }
|
||||
pre.src-org:before { content: 'Org mode'; }
|
||||
pre.src-oz:before { content: 'OZ'; }
|
||||
pre.src-plantuml:before { content: 'Plantuml'; }
|
||||
pre.src-processing:before { content: 'Processing.js'; }
|
||||
pre.src-python:before { content: 'Python'; }
|
||||
pre.src-R:before { content: 'R'; }
|
||||
pre.src-ruby:before { content: 'Ruby'; }
|
||||
pre.src-sass:before { content: 'Sass'; }
|
||||
pre.src-scheme:before { content: 'Scheme'; }
|
||||
pre.src-screen:before { content: 'Gnu Screen'; }
|
||||
pre.src-sed:before { content: 'Sed'; }
|
||||
pre.src-sh:before { content: 'shell'; }
|
||||
pre.src-sql:before { content: 'SQL'; }
|
||||
pre.src-sqlite:before { content: 'SQLite'; }
|
||||
/* additional languages in org.el's org-babel-load-languages alist */
|
||||
pre.src-forth:before { content: 'Forth'; }
|
||||
pre.src-io:before { content: 'IO'; }
|
||||
pre.src-J:before { content: 'J'; }
|
||||
pre.src-makefile:before { content: 'Makefile'; }
|
||||
pre.src-maxima:before { content: 'Maxima'; }
|
||||
pre.src-perl:before { content: 'Perl'; }
|
||||
pre.src-picolisp:before { content: 'Pico Lisp'; }
|
||||
pre.src-scala:before { content: 'Scala'; }
|
||||
pre.src-shell:before { content: 'Shell Script'; }
|
||||
pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
|
||||
/* additional language identifiers per "defun org-babel-execute"
|
||||
in ob-*.el */
|
||||
pre.src-cpp:before { content: 'C++'; }
|
||||
pre.src-abc:before { content: 'ABC'; }
|
||||
pre.src-coq:before { content: 'Coq'; }
|
||||
pre.src-groovy:before { content: 'Groovy'; }
|
||||
/* additional language identifiers from org-babel-shell-names in
|
||||
ob-shell.el: ob-shell is the only babel language using a lambda to put
|
||||
the execution function name together. */
|
||||
pre.src-bash:before { content: 'bash'; }
|
||||
pre.src-csh:before { content: 'csh'; }
|
||||
pre.src-ash:before { content: 'ash'; }
|
||||
pre.src-dash:before { content: 'dash'; }
|
||||
pre.src-ksh:before { content: 'ksh'; }
|
||||
pre.src-mksh:before { content: 'mksh'; }
|
||||
pre.src-posh:before { content: 'posh'; }
|
||||
/* Additional Emacs modes also supported by the LaTeX listings package */
|
||||
pre.src-ada:before { content: 'Ada'; }
|
||||
pre.src-asm:before { content: 'Assembler'; }
|
||||
pre.src-caml:before { content: 'Caml'; }
|
||||
pre.src-delphi:before { content: 'Delphi'; }
|
||||
pre.src-html:before { content: 'HTML'; }
|
||||
pre.src-idl:before { content: 'IDL'; }
|
||||
pre.src-mercury:before { content: 'Mercury'; }
|
||||
pre.src-metapost:before { content: 'MetaPost'; }
|
||||
pre.src-modula-2:before { content: 'Modula-2'; }
|
||||
pre.src-pascal:before { content: 'Pascal'; }
|
||||
pre.src-ps:before { content: 'PostScript'; }
|
||||
pre.src-prolog:before { content: 'Prolog'; }
|
||||
pre.src-simula:before { content: 'Simula'; }
|
||||
pre.src-tcl:before { content: 'tcl'; }
|
||||
pre.src-tex:before { content: 'TeX'; }
|
||||
pre.src-plain-tex:before { content: 'Plain TeX'; }
|
||||
pre.src-verilog:before { content: 'Verilog'; }
|
||||
pre.src-vhdl:before { content: 'VHDL'; }
|
||||
pre.src-xml:before { content: 'XML'; }
|
||||
pre.src-nxml:before { content: 'XML'; }
|
||||
/* add a generic configuration mode; LaTeX export needs an additional
|
||||
(add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
|
||||
pre.src-conf:before { content: 'Configuration File'; }
|
||||
|
||||
table { border-collapse:collapse; }
|
||||
caption.t-above { caption-side: top; }
|
||||
caption.t-bottom { caption-side: bottom; }
|
||||
td, th { vertical-align:top; }
|
||||
th.org-right { text-align: center; }
|
||||
th.org-left { text-align: center; }
|
||||
th.org-center { text-align: center; }
|
||||
td.org-right { text-align: right; }
|
||||
td.org-left { text-align: left; }
|
||||
td.org-center { text-align: center; }
|
||||
dt { font-weight: bold; }
|
||||
.footpara { display: inline; }
|
||||
.footdef { margin-bottom: 1em; }
|
||||
.figure { padding: 1em; }
|
||||
.figure p { text-align: center; }
|
||||
.equation-container {
|
||||
display: table;
|
||||
text-align: center;
|
||||
width: 100%;
|
||||
}
|
||||
.equation {
|
||||
vertical-align: middle;
|
||||
}
|
||||
.equation-label {
|
||||
display: table-cell;
|
||||
text-align: right;
|
||||
vertical-align: middle;
|
||||
}
|
||||
.inlinetask {
|
||||
padding: 10px;
|
||||
border: 2px solid gray;
|
||||
margin: 10px;
|
||||
background: #ffffcc;
|
||||
}
|
||||
#org-div-home-and-up
|
||||
{ text-align: right; font-size: 70%; white-space: nowrap; }
|
||||
textarea { overflow-x: auto; }
|
||||
.linenr { font-size: smaller }
|
||||
.code-highlighted { background-color: #ffff00; }
|
||||
.org-info-js_info-navigation { border-style: none; }
|
||||
#org-info-js_console-label
|
||||
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
|
||||
.org-info-js_search-highlight
|
||||
{ background-color: #ffff00; color: #000000; font-weight: bold; }
|
||||
.org-svg { width: 90%; }
|
||||
/*]]>*/-->
|
||||
</style>
|
||||
<link rel="stylesheet" type="text/css" href="css/styles-from-org.css"/>
|
||||
<link rel="stylesheet" type="text/css" href="css/styles.css"/>
|
||||
<script type="text/javascript">
|
||||
/*
|
||||
@licstart The following is the entire license notice for the
|
||||
JavaScript code in this tag.
|
||||
|
||||
Copyright (C) 2012-2020 Free Software Foundation, Inc.
|
||||
|
||||
The JavaScript code in this tag is free software: you can
|
||||
redistribute it and/or modify it under the terms of the GNU
|
||||
General Public License (GNU GPL) as published by the Free Software
|
||||
Foundation, either version 3 of the License, or (at your option)
|
||||
any later version. The code is distributed WITHOUT ANY WARRANTY;
|
||||
without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
|
||||
|
||||
As additional permission under GNU GPL version 3 section 7, you
|
||||
may distribute non-source (e.g., minimized or compacted) forms of
|
||||
that code without the copy of the GNU GPL normally required by
|
||||
section 4, provided you include this license notice and a URL
|
||||
through which recipients can access the Corresponding Source.
|
||||
|
||||
|
||||
@licend The above is the entire license notice
|
||||
for the JavaScript code in this tag.
|
||||
*/
|
||||
<!--/*--><![CDATA[/*><!--*/
|
||||
function CodeHighlightOn(elem, id)
|
||||
{
|
||||
var target = document.getElementById(id);
|
||||
if(null != target) {
|
||||
elem.cacheClassElem = elem.className;
|
||||
elem.cacheClassTarget = target.className;
|
||||
target.className = "code-highlighted";
|
||||
elem.className = "code-highlighted";
|
||||
}
|
||||
}
|
||||
function CodeHighlightOff(elem, id)
|
||||
{
|
||||
var target = document.getElementById(id);
|
||||
if(elem.cacheClassElem)
|
||||
elem.className = elem.cacheClassElem;
|
||||
if(elem.cacheClassTarget)
|
||||
target.className = elem.cacheClassTarget;
|
||||
}
|
||||
/*]]>*///-->
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="content">
|
||||
|
||||
<div id="outline-container-org43e8363" class="outline-2">
|
||||
<h2 id="org43e8363"><span class="section-number-2">1</span> Overview</h2>
|
||||
<div class="outline-text-2" id="text-1">
|
||||
<p>
|
||||
<a href="https://orgmode.org/">Org-mode</a> and <a href="https://sourceforge.net/projects/matlab-emacs/">matlab-mode</a> provide a very efficient and effective system for creating scientific
|
||||
documents which contain MATLAB code and/or Simulink models along with the results of these. The
|
||||
results of running MATLAB code or simulating Simulink models is placed into the org-mode file by
|
||||
org-mode using org babel. <a href="https://orgmode.org/worg/org-contrib/babel/">Org babel</a> is org-mode's ability to execute source code within org-mode
|
||||
files and optionally insert the results back in to the org-mode file. You define source code in code
|
||||
blocks, e.g.
|
||||
</p>
|
||||
|
||||
<pre class="example">
|
||||
#+begin_src LANGUAGE <OPTIONS>
|
||||
<CODE>
|
||||
#+end_src
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="outline-container-orgb568991" class="outline-2">
|
||||
<h2 id="orgb568991"><span class="section-number-2">2</span> Example</h2>
|
||||
<div class="outline-text-2" id="text-2">
|
||||
<p>
|
||||
With org-mode you can embed semantically colored code such as MATLAB within your document and
|
||||
semantically edit it using "Org -> Editing -> Edit Source Example" menu or <code>C-c '</code>. For example,
|
||||
here's a MATLAB <a href="https://www.mathworks.com/help/matlab/enumeration-classes.html">enumeration class</a>:
|
||||
</p>
|
||||
|
||||
<div class="org-src-container">
|
||||
<pre class="src src-matlab"><span style="color: #a020f0;">classdef</span> <span style="color: #0000ff;">WeekDays</span>
|
||||
<span style="color: #a020f0;">enumeration</span>
|
||||
Monday, Tuesday, Wednesday, Thursday, Friday
|
||||
<span style="color: #a020f0;">end</span>
|
||||
<span style="color: #a020f0;">end</span>
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
You can use org-mode babel to evaluate MATLAB code blocks. For example, if you type <code>C-c C-c</code> in the
|
||||
code block, org-mode will evaluate the code in the <code>*MATLAB*</code> session and insert the value of <code>ans</code>
|
||||
just below the code block.
|
||||
</p>
|
||||
|
||||
<div class="org-src-container">
|
||||
<pre class="src src-matlab">a=1<span style="color: #483d8b;">+</span>3<span style="color: #483d8b;">+</span>5<span style="color: #483d8b;">+</span>7;
|
||||
<span style="color: #008b8b; font-style: italic;">ans</span>=a
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<pre class="example">
|
||||
16
|
||||
</pre>
|
||||
|
||||
|
||||
<p>
|
||||
You can also use org-mode babel evaluate MATLAB code blocks to plot and insert figures back in
|
||||
to this file. For example:
|
||||
</p>
|
||||
|
||||
<div class="org-src-container">
|
||||
<pre class="src src-matlab">t=[0<span style="color: #483d8b;">:</span>.1<span style="color: #483d8b;">:</span>2<span style="color: #483d8b;">*</span><span style="color: #008b8b; font-style: italic;">pi</span>];
|
||||
y=sin(t);
|
||||
<span style="color: #483d8b;">plot</span>(t,y);
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="figure">
|
||||
<p><img src="sinewave.png" alt="sinewave.png" />
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="outline-container-org16c84e6" class="outline-2">
|
||||
<h2 id="org16c84e6"><span class="section-number-2">3</span> HTML Export</h2>
|
||||
<div class="outline-text-2" id="text-3">
|
||||
<p>
|
||||
You need the htmlize package to get coloring in HTML.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
We set the "#+html_head_extra" properties above to configure CSS. We used <code>M-x
|
||||
org-html-htmlize-generate-css</code> to create ./css/styles-from-org.css and added our customizations in
|
||||
./css/styles.css.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="outline-container-orgd802330" class="outline-2">
|
||||
<h2 id="orgd802330"><span class="section-number-2">4</span> Setup</h2>
|
||||
<div class="outline-text-2" id="text-4">
|
||||
<p>
|
||||
The following is known to work with Emacs 27 which includes org-mode 9.3. It is also know to work
|
||||
with Emacs 27 and org-mode 9.6.1.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
To use matlab-mode with org-mode, you need to configure org-mode for matlab-mode. See <a href="../../README.html">README.org</a> to
|
||||
install matlab-mode.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
We configure matlab-mode to use buffer name <code>*MATLAB*</code> and we alter org-mode to use this buffer by
|
||||
adding the following to your <code>~/.emacs</code>.
|
||||
</p>
|
||||
|
||||
<div class="org-src-container">
|
||||
<pre class="src src-emacs-lisp"><span style="color: #b22222;">;;</span><span style="color: #b22222;">-----------------------------------------------------------------------------------------;;</span>
|
||||
<span style="color: #b22222;">;; </span><span style="color: #b22222;">org babel for matlab - make all matlab code blocks execute in the same *MATLAB* session ;;</span>
|
||||
<span style="color: #b22222;">;;</span><span style="color: #b22222;">-----------------------------------------------------------------------------------------;;</span>
|
||||
|
||||
<span style="color: #b22222;">;; </span><span style="color: #b22222;">A better approach to setting org-babel-load-languages is to customize it:</span>
|
||||
<span style="color: #b22222;">;; </span><span style="color: #b22222;">M-x customize-variable RET org-babel-load-languages RET</span>
|
||||
<span style="color: #b22222;">;; </span><span style="color: #b22222;">However, we are doing this here to illustrate how matlab interacts with org</span>
|
||||
(<span style="color: #a020f0;">setq</span> org-babel-load-languages '((emacs-lisp . t)
|
||||
(matlab . t)))
|
||||
|
||||
(<span style="color: #a020f0;">setq</span> org-babel-default-header-args:matlab '((<span style="color: #483d8b;">:session</span> . <span style="color: #8b2252;">"*MATLAB*"</span>)))
|
||||
|
||||
(<span style="color: #a020f0;">defun</span> <span style="color: #0000ff;">matlab-org-session-advice</span> (orig-fun <span style="color: #228b22;">&rest</span> args)
|
||||
<span style="color: #8b2252;">"Advice for org to reuse the *MATLAB* buffer"</span>
|
||||
<span style="color: #b22222;">;; </span><span style="color: #b22222;">ob-octave.el leverages both org-babel-matlab-emacs-link-wrapper-method and</span>
|
||||
<span style="color: #b22222;">;; </span><span style="color: #b22222;">org-babel-octave-wrapper-method when interacting with the *MATLAB* buffer.</span>
|
||||
<span style="color: #b22222;">;; </span><span style="color: #b22222;">Here we fix a couple items such as adding cd default-directory:</span>
|
||||
(<span style="color: #a020f0;">setq</span> org-babel-matlab-emacs-link-wrapper-method
|
||||
(concat <span style="color: #8b2252;">"\</span>
|
||||
<span style="color: #8b2252;"> cd('"</span> default-directory <span style="color: #8b2252;">"');</span>
|
||||
<span style="color: #8b2252;"> %s</span>
|
||||
<span style="color: #8b2252;"> if ~exist('ans','var') ans=''; end</span>
|
||||
<span style="color: #8b2252;"> if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);</span>
|
||||
<span style="color: #8b2252;"> else, save -ascii %s ans</span>
|
||||
<span style="color: #8b2252;"> end</span>
|
||||
<span style="color: #8b2252;"> delete('%s')</span>
|
||||
<span style="color: #8b2252;"> "</span>))
|
||||
(<span style="color: #a020f0;">setq</span> org-babel-octave-wrapper-method
|
||||
(concat <span style="color: #8b2252;">"\</span>
|
||||
<span style="color: #8b2252;"> cd('"</span> default-directory <span style="color: #8b2252;">"');</span>
|
||||
<span style="color: #8b2252;"> %s</span>
|
||||
<span style="color: #8b2252;"> if ~exist('ans','var') ans=''; end</span>
|
||||
<span style="color: #8b2252;"> if ischar(ans) || isstring(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);</span>
|
||||
<span style="color: #8b2252;"> else, dlmwrite('%s', ans, '\\t')</span>
|
||||
<span style="color: #8b2252;"> end"</span>))
|
||||
(apply orig-fun args))
|
||||
|
||||
(<span style="color: #a020f0;">defun</span> <span style="color: #0000ff;">matlab-org-fixup-print</span> (orig-fun session body result-type <span style="color: #228b22;">&optional</span> matlabp)
|
||||
<span style="color: #8b2252;">"Fixup figure print to make it work with MATLAB"</span>
|
||||
<span style="color: #b22222;">;; </span><span style="color: #b22222;">org 9.3 correctly does: print -dpng figure.png</span>
|
||||
<span style="color: #b22222;">;; </span><span style="color: #b22222;">org 9.6.1 incorrectly does: print -dpng "figure.png"</span>
|
||||
<span style="color: #b22222;">;; </span><span style="color: #b22222;">and thus 9.6.1 creates on disk a file name containing quotes which is incorrect, so this</span>
|
||||
<span style="color: #b22222;">;; </span><span style="color: #b22222;">advice fixes that.</span>
|
||||
(<span style="color: #a020f0;">setq</span> body (replace-regexp-in-string <span style="color: #8b2252;">"^</span><span style="color: #8b2252; font-weight: bold;">\\</span><span style="color: #8b2252; font-weight: bold;">(</span><span style="color: #8b2252;">print -dpng </span><span style="color: #8b2252; font-weight: bold;">\\</span><span style="color: #8b2252; font-weight: bold;">)</span><span style="color: #8b2252;">\"</span><span style="color: #8b2252; font-weight: bold;">\\</span><span style="color: #8b2252; font-weight: bold;">(</span><span style="color: #8b2252;">[</span><span style="color: #8b2252;">^</span><span style="color: #8b2252;">\"]+</span><span style="color: #8b2252; font-weight: bold;">\\</span><span style="color: #8b2252; font-weight: bold;">)</span><span style="color: #8b2252;">\""</span> <span style="color: #8b2252;">"\\1\\2"</span> body t))
|
||||
(funcall orig-fun session body result-type matlabp))
|
||||
|
||||
(<span style="color: #a020f0;">defun</span> <span style="color: #0000ff;">org-export-dispatch-no-babel-advice</span> (orig-fun <span style="color: #228b22;">&rest</span> args)
|
||||
<span style="color: #8b2252;">"Instruct babel to not evaluate code blocks (and hence no</span>
|
||||
<span style="color: #8b2252;">prompt) during export, e.g. conversion of org to say html."</span>
|
||||
(<span style="color: #a020f0;">let*</span> ((org-babel-default-header-args
|
||||
(cons '(<span style="color: #483d8b;">:eval</span> . <span style="color: #8b2252;">"never-export"</span>) org-babel-default-header-args))
|
||||
(result (apply orig-fun args)))
|
||||
result))
|
||||
|
||||
(eval-after-load 'ox
|
||||
'(<span style="color: #a020f0;">progn</span>
|
||||
<span style="color: #b22222;">;; </span><span style="color: #b22222;">Make C-c C-e `</span><span style="color: #008b8b;">org-export-dispatch</span><span style="color: #b22222;">' work without prompting to evaluate code blocks</span>
|
||||
(advice-add 'org-export-dispatch <span style="color: #483d8b;">:around</span> #'org-export-dispatch-no-babel-advice)))
|
||||
|
||||
<span style="color: #b22222;">;; </span><span style="color: #b22222;">org babel for matlab - make all matlab code blocks execute in the same *MATLAB* session</span>
|
||||
(eval-after-load <span style="color: #8b2252;">"org"</span>
|
||||
'(<span style="color: #a020f0;">progn</span>
|
||||
(advice-add 'org-babel-octave-evaluate-external-process <span style="color: #483d8b;">:around</span> #'matlab-org-session-advice)
|
||||
(advice-add 'org-babel-octave-evaluate-session <span style="color: #483d8b;">:around</span> #'matlab-org-session-advice)
|
||||
(advice-add 'org-babel-octave-evaluate <span style="color: #483d8b;">:around</span> #'matlab-org-fixup-print)))
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
To try this using "stock" emacs, place the above in org-matlab-setup.el and use:
|
||||
</p>
|
||||
|
||||
<pre class="example">
|
||||
emacs -q -L /path/to/matlab-mode -l /path/to/matlab-load.el -l org-matlab-setup.el
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="postamble" class="status">
|
||||
<p class="author">Author: John Ciolfi</p>
|
||||
<p class="date">Created: 2023-04-10 Mon 15:58</p>
|
||||
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,169 @@
|
|||
# // File: matlab-emacs-src/examples/matlab-and-org-mode/matlab-and-org-mode.org
|
||||
# // Abstract:
|
||||
# // Use this as a template for creating org-files with MATLAB and other language code blocks
|
||||
|
||||
#+startup: showall
|
||||
#+startup: inlineimages // C-c C-x C-v to toggle, C-c C-x C-M-v to redisplay
|
||||
#+startup: latexpreview // C-c C-x C-l to toggle
|
||||
|
||||
#+html_head_extra: <link rel="stylesheet" type="text/css" href="css/styles-from-org.css"/>
|
||||
#+html_head_extra: <link rel="stylesheet" type="text/css" href="css/styles.css"/>
|
||||
#+options: ^:{}
|
||||
#+options: toc:nil
|
||||
#+latex_header: \usepackage[margin=0.5in]{geometry}
|
||||
#+latex_header: \usepackage{parskip}
|
||||
#+latex_header: \usepackage{tocloft}
|
||||
#+latex_header: \advance\cftsecnumwidth 0.5em\relax
|
||||
#+latex_header: \advance\cftsubsecindent 0.5em\relax
|
||||
#+latex_header: \advance\cftsubsecnumwidth 0.5em\relax
|
||||
|
||||
* Overview
|
||||
|
||||
[[https://orgmode.org/][Org-mode]] and [[https://sourceforge.net/projects/matlab-emacs/][matlab-mode]] provide an efficient and effective system for creating scientific documents
|
||||
which contain MATLAB code and/or Simulink models along with the results of these. The results of
|
||||
running MATLAB code or simulating Simulink models is placed into the org-mode file by org-mode using
|
||||
org babel. [[https://orgmode.org/worg/org-contrib/babel/][Org babel]] is org-mode's ability to execute source code within org-mode files and
|
||||
optionally insert the results back in to the org-mode file. You define source code in code blocks,
|
||||
e.g.
|
||||
|
||||
: #+begin_src LANGUAGE <OPTIONS>
|
||||
: <CODE>
|
||||
: #+end_src
|
||||
|
||||
* Example
|
||||
|
||||
With org-mode you can embed semantically colored code such as MATLAB within your document and
|
||||
semantically edit it using "Org -> Editing -> Edit Source Example" menu or =C-c '=. For example,
|
||||
here's a MATLAB [[https://www.mathworks.com/help/matlab/enumeration-classes.html][enumeration class]]:
|
||||
|
||||
#+begin_src matlab
|
||||
classdef WeekDays
|
||||
enumeration
|
||||
Monday, Tuesday, Wednesday, Thursday, Friday
|
||||
end
|
||||
end
|
||||
#+end_src
|
||||
|
||||
You can use org-mode babel to evaluate MATLAB code blocks. For example, if you type =C-c C-c= in the
|
||||
following code block, org-mode will evaluate the code in the =*MATLAB*= session and insert the value
|
||||
of =ans= just below the code block.
|
||||
|
||||
#+begin_src matlab :exports both :results verbatim
|
||||
a=1+3+5+7;
|
||||
ans=a
|
||||
#+end_src
|
||||
|
||||
#+RESULTS:
|
||||
: 16
|
||||
|
||||
You can also use org-mode babel evaluate MATLAB code blocks to plot and insert figures back in to
|
||||
this file. For example:
|
||||
|
||||
#+begin_src matlab :file sinewave.png :exports both :results file graphics
|
||||
t=[0:.1:2*pi];
|
||||
y=sin(t);
|
||||
plot(t,y);
|
||||
#+end_src
|
||||
|
||||
#+RESULTS:
|
||||
[[file:sinewave.png]]
|
||||
* Export to HTML, PDF, etc.
|
||||
|
||||
You need the htmlize package to get coloring.
|
||||
|
||||
For HTML export we set the "#+html_head_extra" properties above to configure CSS. We used ~M-x
|
||||
org-html-htmlize-generate-css~ to create ./css/styles-from-org.css and added our customizations in
|
||||
./css/styles.css.
|
||||
|
||||
For PDF export we configured several "#+latex_header" properties above.
|
||||
|
||||
After this setup, you can use the "Org -> Export/Publish" or ~C-c C-e~ to export to HTML, PDF, etc.
|
||||
|
||||
See the example *.html in this directory.
|
||||
|
||||
* Setup
|
||||
|
||||
The following is known to work with Emacs 27 which includes org-mode 9.3. It is also know to work
|
||||
with Emacs 27 and org-mode 9.6.1.
|
||||
|
||||
To use matlab-mode with org-mode, you need to configure org-mode for matlab-mode. See [[file:../../README.org][README.org]] to
|
||||
install matlab-mode.
|
||||
|
||||
We configure matlab-mode to use buffer name =*MATLAB*= and we alter org-mode to use this buffer by
|
||||
adding the following to your =~/.emacs=.
|
||||
|
||||
#+begin_src emacs-lisp
|
||||
;;-----------------------------------------------------------------------------------------;;
|
||||
;; org babel for matlab - make all matlab code blocks execute in the same *MATLAB* session ;;
|
||||
;;-----------------------------------------------------------------------------------------;;
|
||||
|
||||
;; A better approach to setting org-babel-load-languages is to customize it:
|
||||
;; M-x customize-variable RET org-babel-load-languages RET
|
||||
;; However, we are doing this here to illustrate how matlab interacts with org
|
||||
(setq org-babel-load-languages '((emacs-lisp . t)
|
||||
(matlab . t)))
|
||||
|
||||
(setq org-babel-default-header-args:matlab '((:session . "*MATLAB*")))
|
||||
|
||||
(defun matlab-org-session-advice (orig-fun &rest args)
|
||||
"Advice for org to reuse the *MATLAB* buffer"
|
||||
;; ob-octave.el leverages both org-babel-matlab-emacs-link-wrapper-method and
|
||||
;; org-babel-octave-wrapper-method when interacting with the *MATLAB* buffer.
|
||||
;; Here we fix a couple items such as adding cd default-directory:
|
||||
(setq org-babel-matlab-emacs-link-wrapper-method
|
||||
(concat "\
|
||||
cd('" default-directory "');
|
||||
%s
|
||||
if ~exist('ans','var') ans=''; end
|
||||
if ischar(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
|
||||
else, save -ascii %s ans
|
||||
end
|
||||
delete('%s')
|
||||
"))
|
||||
(setq org-babel-octave-wrapper-method
|
||||
(concat "\
|
||||
cd('" default-directory "');
|
||||
%s
|
||||
if ~exist('ans','var') ans=''; end
|
||||
if ischar(ans) || isstring(ans), fid = fopen('%s', 'w'); fprintf(fid, '%%s\\n', ans); fclose(fid);
|
||||
else, dlmwrite('%s', ans, '\\t')
|
||||
end"))
|
||||
(apply orig-fun args))
|
||||
|
||||
(defun matlab-org-fixup-print (orig-fun session body result-type &optional matlabp)
|
||||
"Fixup figure print to make it work with MATLAB"
|
||||
;; org 9.3 correctly does: print -dpng figure.png
|
||||
;; org 9.6.1 incorrectly does: print -dpng "figure.png"
|
||||
;; and thus 9.6.1 creates on disk a file name containing quotes which is incorrect, so this
|
||||
;; advice fixes that.
|
||||
(setq body (replace-regexp-in-string "^\\(print -dpng \\)\"\\([^\"]+\\)\"" "\\1\\2" body t))
|
||||
(funcall orig-fun session body result-type matlabp))
|
||||
|
||||
(defun org-export-dispatch-no-babel-advice (orig-fun &rest args)
|
||||
"Instruct babel to not evaluate code blocks (and hence no
|
||||
prompt) during export, e.g. conversion of org to say html."
|
||||
(let* ((org-babel-default-header-args
|
||||
(cons '(:eval . "never-export") org-babel-default-header-args))
|
||||
(result (apply orig-fun args)))
|
||||
result))
|
||||
|
||||
(eval-after-load 'ox
|
||||
'(progn
|
||||
;; Make C-c C-e `org-export-dispatch' work without prompting to evaluate code blocks
|
||||
(advice-add 'org-export-dispatch :around #'org-export-dispatch-no-babel-advice)))
|
||||
|
||||
;; org babel for matlab - make all matlab code blocks execute in the same *MATLAB* session
|
||||
(eval-after-load "org"
|
||||
'(progn
|
||||
(advice-add 'org-babel-octave-evaluate-external-process :around #'matlab-org-session-advice)
|
||||
(advice-add 'org-babel-octave-evaluate-session :around #'matlab-org-session-advice)
|
||||
(advice-add 'org-babel-octave-evaluate :around #'matlab-org-fixup-print)))
|
||||
#+end_src
|
||||
|
||||
To try this using "stock" emacs, place the above in org-matlab-setup.el and use:
|
||||
|
||||
: emacs -q -L /path/to/matlab-mode -l /path/to/matlab-load.el -l org-matlab-setup.el
|
||||
|
||||
# LocalWords: showall inlineimages latexpreview usepackage parskip tocloft cftsecnumwidth sinewave
|
||||
# LocalWords: cftsubsecindent cftsubsecnumwidth setq defun isstring fixup matlabp dpng funcall
|
||||
# LocalWords: progn htmlize
|
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
Loading…
Reference in New Issue