CINXE.COM

MATLAB Scripting

<!DOCTYPE html> <html> <head> <title>MATLAB Scripting</title> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=3" /> <meta content="ImageJ Wiki" property="og:site_name"> <meta content="MATLAB Scripting" property="og:title"> <meta content="article" property="og:type"> <meta content="The ImageJ wiki is a community-edited knowledge base on topics relating to ImageJ, a public domain program for processing and analyzing scientific images, and its ecosystem of derivatives and variants, including ImageJ2, Fiji, and others." property="og:description"> <meta content="https://imagej.github.io/scripting/matlab" property="og:url"> <meta content="https://imagej.github.io/media/icons/imagej2.png" property="og:image"> <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"> <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png"> <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"> <link rel="manifest" href="/site.webmanifest"> <link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5"> <meta name="msapplication-TileColor" content="#da532c"> <meta name="theme-color" content="#ffffff"> <link rel="stylesheet" href="/assets/css/lightbox.min.css" /> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/instantsearch.css@7/themes/algolia-min.css"/> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/dygraphs@2.1.0/dist/dygraph.min.js"></script> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/dygraphs@2.1.0/dist/dygraph.min.css" /> <link rel="stylesheet" href="/assets/css/main.css" /> <link rel="stylesheet" href="/assets/css/layout.css"/> <link rel="stylesheet" href="/assets/css/includes.css" /> <link rel="stylesheet" href="/assets/css/timeline.css"/> <link rel="stylesheet" href="/assets/css/dock.css"/> <noscript><link rel="stylesheet" href="/assets/css/noscript.css" /></noscript> <link rel="stylesheet" href="/assets/css/page.css" /> </head> <body class="is-preload"> <!-- Top bar --> <section id="top-bar"> <a href="/"><img id="site-logo" src="/media/icons/imagej2.png"></a> <div id="search-panel"> <div id="search-box"></div> </div> </section> <!-- Page-info --> <div class="page-info"> <a href="https://github.com/imagej/imagej.github.io/commits/main/_pages/scripting/matlab.md">Page history</a> <a href="https://github.com/imagej/imagej.github.io/edit/main/_pages/scripting/matlab.md">Edit this page</a> <a href="/editing">How do I edit this website?</a></div> <section id="left-column" class="sidebar dock"> <!-- Menu --> <div class="menu dockable"> <div class="drag-handle"></div><h3>ImageJ Docs</h3> <ul> <li><a href="/downloads">Download</a></li> <li><details><summary>Learn</summary><ul> <li><details><summary><a href="/learn">ImageJ Basics</a></summary><ul> <li><a href="/tutorials">Tutorials</a></li> <li><a href="/learn/user-guides">User Guides</a></li> <li><a href="/learn/keyboard-shortcuts">Keyboard Shortcuts</a></li> <li><a href="/learn/tips-and-tricks">Tips and Tricks</a></li> <li><a href="/learn/troubleshooting">Troubleshooting</a></li> <li><a href="/learn/faq">Frequently Asked Questions</a></li> <li><details><summary><a href="/platforms">Supported Platforms</a></summary><ul> <li><a href="/platforms/windows">Windows</a></li> <li><a href="/platforms/macos">MacOS</a></li> <li><a href="/platforms/linux">Linux</a></li> <li><a href="/platforms/pi">Raspberry Pi</a></li> <li><a href="/platforms/android">Android</a></li> </ul></details></li> <!-- Learn/ImageJ Basics/Supported Platforms --> <li><details><summary><a href="/formats">File Formats</a></summary><ul> <li><a href="/formats/bio-formats">Bio-Formats</a></li> <li><a href="/formats/video">Video formats</a></li> <li><a href="/formats/quicktime">QuickTime</a></li> <li><a href="/formats/tiff">TIFF</a></li> <li><a href="/formats/pdf">PDF</a></li> <li><a href="/formats/dicom">DICOM</a></li> <li><a href="/formats/lsm">Zeiss LSM</a></li> <li><a href="/formats/olympus">Olympus VSI</a></li> <li><a href="/formats/lurawave">Opera Flex</a></li> <li><a href="/formats/fib-sem">FIB-SEM</a></li> </ul></details></li> <!-- Learn/ImageJ Basics/File Formats --> </ul></details></li> <!-- Learn/ImageJ --> <li><details><summary><a href="/imaging">Scientific Imaging</a></summary><ul> <li><a href="/imaging/principles">Principles of Scientific Imaging</a></li> <li><a href="/imaging/annotating-images">Annotating Images</a></li> <li><a href="/imaging/colocalization-analysis">Colocalization</a></li> <li><a href="/imaging/color-image-processing">Color Image Processing</a></li> <li><a href="/imaging/deconvolution">Deconvolution</a></li> <li><a href="/imaging/image-intensity-processing">Image Intensity Processing</a></li> <li><a href="/imaging/particle-analysis">Particle Analysis</a></li> <li><a href="/imaging/registration">Registration</a></li> <li><a href="/imaging/segmentation">Segmentation</a></li> <li><a href="/imaging/stack-slice-manipulations">Stack-slice Manipulations</a></li> <li><a href="/imaging/t-functions">T-functions</a></li> <li><a href="/imaging/tracking">Tracking</a></li> <li><a href="/imaging/visualization">Visualization</a></li> <li><a href="/imaging/z-functions">Z-functions</a></li> </ul></details></li> <!-- Learn/Scientific Imaging --> </ul></details></li> <!-- Learn --> <li><details open><summary><a href="/plugins">Extend</a></summary><ul> <li><a href="/list-of-extensions">List of Extensions</a></li> <li><details><summary><a href="/update-sites">Update Sites</a></summary><ul> <li><a href="/list-of-update-sites">List of Update Sites</a></li> <li><a href="/update-sites/following">Following an Update Site</a></li> <li><a href="/update-sites/setup">Creating an Update Site</a></li> <li><a href="/update-sites/tos">Terms of Service</a></li> <li><a href="/update-sites/automatic-uploads">Automatic Upload</a></li> <li><a href="/update-sites/core-uploads">Uploading to Core Sites</a></li> <li><a href="/update-sites/faq">Update Sites FAQ</a></li> <li><a href="/update-sites/stats">Statistics</a></li> </ul></details></li> <!-- Extend/Update Sites --> <li><details open><summary><a href="/scripting">Scripting</a></summary><ul> <li><a href="/scripting/basics">Scripting Basics</a></li> <li><a href="/scripting/script-editor">Script Editor</a></li> <li><a href="/scripting/parameters">Parameters</a></li> <li><a href="/scripting/user-input">User Input</a></li> <li><a href="/scripting/auto-imports">Auto Import</a></li> <li><a href="/scripting/templates">Templates</a></li> <li><a href="/scripting/batch">Batch Processing</a></li> <li><a href="/scripting/headless">Running Headlessly</a></li> <li><a href="/scripting/comparisons">Scripting Comparisons</a></li> <li><a href="/scripting/toolbox">Toolbox</a></li> <li><details open><summary>Languages</summary><ul> <li><a href="/scripting/beanshell">BeanShell Scripting</a></li> <li><a href="/scripting/groovy">Groovy Scripting</a></li> <li><a href="/scripting/macro">ImageJ Macro</a></li> <li><a href="/scripting/javascript">JavaScript</a></li> <li><a href="/scripting/clojure">Lisp (Clojure)</a></li> <li><a class="current-page">MATLAB</a></li> <li><a href="/scripting/jython">Python (Jython)</a></li> <li><a href="/scripting/python">Python (native)</a></li> <li><a href="/scripting/renjin">R (Renjin)</a></li> <li><a href="/scripting/jruby">Ruby (JRuby)</a></li> <li><a href="/scripting/scala">Scala Scripting</a></li> </ul></details></li> <!-- Extend/Scripting/Languages --> </ul></details></li> <!-- Extend/Scripting --> <li><details><summary><a href="/develop">Development</a></summary><ul> <li><a href="/develop/philosophy">Philosophy</a></li> <li><a href="/develop/architecture">Architecture</a></li> <li><a href="/develop/source">Source code</a></li> <li><a href="/develop/project-management">Project management</a></li> <li><a href="/develop/coding-style">Coding style</a></li> <li><a href="/develop/javadoc">Using Javadoc</a></li> <li><a href="/develop/debugging">Debugging</a></li> <li><a href="/develop/wish-list">Wish list</a></li> <li><details><summary>Tools</summary><ul> <li><a href="/develop/github">GitHub</a></li> <li><a href="/develop/maven">Maven</a></li> <li><a href="/develop/ci">CI/CD</a></li> <li><a href="/develop/dotfiles">Dotfiles</a></li> <li><details><summary><a href="/develop/ides">IDEs</a></summary><ul> <li><a href="/develop/eclipse">Eclipse</a></li> <li><a href="/develop/netbeans">NetBeans</a></li> <li><a href="/develop/intellij">IntelliJ IDEA</a></li> <li><a href="/develop/command-line">Command Line</a></li> </ul></details></li> </ul></details></li> <!-- Extend/Development/Tools --> <li><details><summary>Guides</summary><ul> <li><a href="/develop/plugins">Writing plugins</a></li> <li><a href="/develop/improving-the-code">Contributing to a plugin</a></li> <li><a href="/develop/releasing">Development lifecycle</a></li> <li><a href="/develop/building-a-pom">Building a POM</a></li> <li><a href="/develop/debugging-exercises">Hands-on debugging</a></li> <li><a href="/develop/writing-ops">Adding new ops</a></li> <li><a href="/develop/formats">Adding new formats</a></li> <li><a href="/develop/native-libraries">Using native libraries</a></li> <li><a href="/develop/tips">Tips for developers</a></li> <li><a href="/develop/cpp-tips">Tips for C++ developers</a></li> <li><a href="/develop/ij1-plugins">ImageJ 1.x plugins</a></li> <li><a href="/develop/versioning">Versioning</a></li> <li><a href="/develop/logging">Logging</a></li> <li><a href="/develop/uber-jars">Uber-JARs</a></li> </ul></details></li> <!-- Extend/Development/Guides --> <li><details><summary><a href="/develop/git">Git</a></summary><ul> <li><a href="/develop/git/eclipse">Git in Eclipse (EGit)</a></li> <li><a href="/develop/git/mini-howto">Git mini howto</a></li> <li><a href="/develop/git/workshop">Git workshop</a></li> <li><a href="/develop/git/conflicts">Git conflicts</a></li> <li><a href="/develop/git/topic-branches">Git topic branches</a></li> <li><a href="/develop/git/notes">Git notes</a></li> <li><a href="/develop/git/reflogs">Git reflogs</a></li> <li><a href="/develop/git/submodules">Git submodules</a></li> <li><a href="/develop/git/pinpoint-regressions">How to pinpoint regressions</a></li> <li><a href="/develop/git/publish-a-repository">How to publish a git repository</a></li> <li><a href="/develop/git/extract-a-subproject">How to extract a subproject</a></li> </ul></details></li> <!-- Extend/Development/Git --> </ul></details></li> <!-- Extend/Development --> </ul></details></li> <!-- Extend --> <li><details><summary><a href="/contribute">Contribute</a></summary><ul> <li><a href="/contribute/citing">Citing</a></li> <li><a href="/people">Contributors</a></li> <li><a href="/orgs">Organizations</a></li> <li><a href="/contribute/governance">Governance</a></li> <li><a href="/contribute/funding">Funding</a></li> <li><a href="/contribute/fiji">Contributing to Fiji</a></li> <li><details><summary><a href="/licensing">Licensing</a></summary><ul> <li><details><summary><a href="/licensing/open-source">Open Source</a></summary><ul> <li><a href="/licensing/apache">Apache</a></li> <li><a href="/licensing/bsd">BSD</a></li> <li><a href="/licensing/epl">EPL</a></li> <li><a href="/licensing/gpl">GPL</a></li> <li><a href="/licensing/lgpl">LGPL</a></li> <li><a href="/licensing/mit">MIT</a></li> <li><a href="/licensing/public-domain">Public domain</a></li> <li><a href="/licensing/big">BIG</a></li> </ul></details></li> <!-- Contribute/Licensing/Open Source --> <li><a href="/licensing/closed-source">Proprietary</a></li> </ul></details></li> <!-- Contribute/Licensing --> <li><details><summary><a href="/editing">Editing the Wiki</a></summary><ul> <li><a href="/editing/advanced">Advanced Editing</a></li> <li><a href="/editing/buttons">Buttons</a></li> <li><a href="/editing/citations">Citations</a></li> <li><a href="/editing/code">Source Code</a></li> <li><a href="/editing/debugging">Debugging</a></li> <li><a href="/editing/headers">Headers</a></li> <li><a href="/editing/icons">Icons</a></li> <li><a href="/editing/images">Images</a></li> <li><a href="/editing/keys">Keyboard Shortcuts</a></li> <li><a href="/editing/linking">Linking</a></li> <li><a href="/editing/math">Math Expressions</a></li> <li><a href="/editing/menu-paths">Menu Paths</a></li> <li><a href="/editing/navigation">Navigation</a></li> <li><a href="/editing/notices">Notices</a></li> <li><a href="/editing/people">People</a></li> <li><a href="/editing/pitfalls">Pitfalls</a></li> <li><a href="/editing/quizzes">Quizzes</a></li> <li><a href="/editing/statbox">Statbox</a></li> <li><a href="/editing/symbols">Symbols</a></li> <li><a href="/editing/tables">Tables</a></li> <li><a href="/editing/timelines">Timelines</a></li> <li><a href="/editing/tooltips">Tooltips</a></li> <li><a href="/editing/videos">Videos</a></li> <li><a href="/editing/whitespace">Whitespace</a></li> </ul></details></li> <!-- Contribute/Editing the Wiki --> </ul></details></li> <!-- Contribute --> <li><details><summary><a href="/discuss">Discuss</a></summary><ul> <li><a href="/discuss/bugs">Reporting Issues</a></li> <li><a href="/discuss/mailing-lists">Mailing Lists</a></li> <li><a href="/discuss/chat">Chat</a></li> </ul></details></li> <!-- Discuss --> <li><details><summary>Explore</summary><ul> <li><a href="/news">News</a></li> <li><details><summary><a href="/events">Events</a></summary><ul> <li><a href="/events/presentations">Presentations</a></li> <li><a href="/events/conferences">Conferences</a></li> <li><a href="/events/hackathons">Hackathons</a></li> <li><a href="/events">More...</a></li> </ul></details></li> <!-- Discuss/Events --> <li><details><summary><a href="/libs">Libraries</a></summary><ul> <li><a href="/libs/imagej-ops">ImageJ Ops</a></li> <li><a href="/libs/imagej-common">ImageJ Common</a></li> <li><a href="/libs/imagej-legacy">ImageJ Legacy</a></li> <li><a href="/libs/scijava">SciJava</a></li> <li><a href="/libs/scifio">SCIFIO</a></li> <li><details><summary><a href="/libs/imglib2">ImgLib2</a></summary><ul> <li><a href="/libs/imglib2/getting-started">Getting Started</a></li> <li><a href="/libs/imglib2/accessors">Accessors</a></li> <li><a href="/libs/imglib2/accessibles">Accessibles</a></li> <li><a href="/libs/imglib2/examples">Examples</a></li> <li><a href="/libs/imglib2/workshop-introductory">Introductory Workshop</a></li> <li><a href="/libs/imglib2/workshop-advanced">Advanced Workshop</a></li> <li><a href="/libs/imglib2/matlab">ImgLib2 images in MATLAB</a></li> <li><a href="/libs/imglib2/benchmarks">Benchmarks</a></li> <li><a href="/libs/imglib2/faq">FAQ</a></li> <li><a href="/libs/imglib2/developing">Developing ImgLib2</a></li> <li><a href="/libs/imglib2/discussion">ImgLib2 Discussion</a></li> </ul></details></li> <!-- Explore/Libraries/ImgLib2 --> <li><a href="/libs">More...</a></li> </ul></details></li> <!-- Explore/Libraries --> <li><details><summary><a href="/software">Software</a></summary><ul> <li><a href="/software/nih-image">NIH Image</a></li> <li><a href="/software/imagej">ImageJ</a></li> <li><a href="/software/imagej2">ImageJ2</a></li> <li><a href="/software/fiji">Fiji</a></li> <li><a href="/software">More...</a></li> </ul></details></li> <!-- Explore/Software --> </ul></details></li> <!-- Explore --> </ul> </div> </section> <section id="right-column" class="sidebar dock"> <!-- Vital stats --> <script src="/assets/js/maven.js"></script> <script> function refreshComponent() { var statboxName = document.getElementById('statbox-name'); if (statboxName) statboxName.innerHTML = 'Reloading...'; fillStatsFromArtifact('net.imagej:imagej-matlab', ''); } refreshComponent(); </script><div id="statbox" class="dockable" style="position: relative;"> <h3>Vital statistics</h3> <div class="drag-handle"></div> <button class="btn-refresh" onclick="refreshComponent()"><i class="fa fa-sync" style="pointer-events: none;"></i></button> <table><tr><td id="statbox-header" colspan=2><p id="statbox-image"><img src="/media/icons/imagej2.png"></p> <p id="statbox-name">Loading...</p></td></tr><tr><th>Source</th> <td id="component-source">none</td></tr><tr><th>License</th> <td id="component-license"></td></tr><tr><th>Release</th> <td id="component-release"></td></tr><tr><th>Date</th> <td id="component-date"></td></tr><tr><th>Development status</th> <td id="component-dev-status"></td></tr><tr><th>Support status</th> <td id="component-support-status"></td></tr><tr><td id="component-team" colspan=2> <details> <summary style="text-align: center"><span class="tooltip">Team<span class="tooltiptext" style="top: 80%; left: -75px; width: 180px">The group of people who take responsibility for the project.</span></span></summary> <table><tr><th><span class="tooltip">Founders<span class="tooltiptext" style="top: 80%; left: -10px; width: 180px">People who created the project. Does not imply any current participation or responsibility.</span></span></th><td id="component-team-founders"></td> </tr><tr><th><span class="tooltip">Leads<span class="tooltiptext" style="top: 80%; left: -10px; width: 180px">People responsible for making decisions about the project: timing of releases, inclusion of features, etc.</span></span></th><td id="component-team-leads"></td> </tr><tr><th><span class="tooltip">Developers<span class="tooltiptext" style="top: 80%; left: -10px; width: 180px">People responsible for adding new features or enhancements.</span></span></th><td id="component-team-developers"></td> </tr><tr><th><span class="tooltip">Debuggers<span class="tooltiptext" style="top: 80%; left: -10px; width: 180px">People responsible for fixing bugs.</span></span></th><td id="component-team-debuggers"></td> </tr><tr><th><span class="tooltip">Reviewers<span class="tooltiptext" style="top: 80%; left: -10px; width: 180px">People responsible for reviewing patch submissions.</span></span></th><td id="component-team-reviewers"></td> </tr><tr><th><span class="tooltip">Support<span class="tooltiptext" style="top: 80%; left: -10px; width: 180px">People responsible for responding to community questions and issue reports.</span></span></th><td id="component-team-support"></td> </tr><tr><th><span class="tooltip">Maintainers<span class="tooltiptext" style="top: 80%; left: -10px; width: 180px">People responsible for maintaining the project: e.g., merging patch submissions and cutting releases.</span></span></th><td id="component-team-maintainers"></td> </tr><tr><th><span class="tooltip">Contributors<span class="tooltiptext" style="top: 80%; left: -10px; width: 180px">People who contributed code to the project. Does not imply any current participation or responsibility.</span></span></th><td id="component-team-contributors"></td> </tr></table> </details> </td></tr></table> </div> <!-- Table of contents --> <div id="toc" class="toc menu dockable"> <div class="drag-handle"></div> <h3>Page contents</h3> <ul data-toc="#content" data-toc-headings="h1,h2,h3,h4,h5,h6"></ul> </div> </section> <!-- Page title --> <div class="container"> <header class="major special"> <a class="project-icon shadowed-box" href="/software/imagej2"><span class="tooltip"><img src="/media/icons/imagej2.png"><span class="tooltiptext" style="top: 80%; left: 25%; width: 20em">This page describes content relating to the ImageJ2 platform. Click the logo for details.</span></span></a> <h1>MATLAB Scripting</h1> <span class="project-counterweight"> </span> </header> </div> <!-- Nav bar --> <!-- Main content --> <section id="page" class="main style1"> <div class="container"> <div class="box alt"></div> <div id="content" class="page-content"> <div class="notice" style="font-size: 2; background: #ffcccb; border-left: 10px solid #f57900"><div class="notice-icon"><img src="/media/icons/warning.png" /></div><div class="notice-content"><p>Prior to MATLAB R2017b, MATLAB ships with Java 7, but ImageJ2 requires Java 8. You will need to change your MATLAB installation to use Java 8, by following the instructions for your platform: <a href="http://www.mathworks.com/matlabcentral/answers/130359-how-do-i-change-the-java-virtual-machine-jvm-that-matlab-is-using-on-windows">Windows</a>, <a href="http://www.mathworks.com/matlabcentral/answers/103056-how-do-i-change-the-java-virtual-machine-jvm-that-matlab-is-using-for-mac-os">macOS</a> or <a href="http://www.mathworks.com/matlabcentral/answers/130360-how-do-i-change-the-java-virtual-machine-jvm-that-matlab-is-using-for-linux-os">Linux</a>.</p> <p>If you run MATLAB R2017b and later, you don’t need to do this, as it ships and uses Java 8 already.</p> </div> </div> <p><strong>ImageJ-MATLAB</strong> is an extension which:</p> <ul> <li>Translates data between ImageJ2 images and MATLAB matrices.</li> <li>Enables execution of MATLAB scripts from inside ImageJ2’s <a href="/scripting/script-editor">Script Editor</a>.</li> <li>Lets you launch ImageJ2 and interact with it from inside MATLAB.</li> <li>Allows developers to write additional <a href="/plugins">plugins</a> which <a href="/develop/architecture#extensibility">extend</a> these capabilities in new directions.</li> </ul> <h1 id="matlab-tutorial-for-imagej2">MATLAB tutorial for ImageJ2</h1> <h2 id="prerequisites">Prerequisites</h2> <ul> <li>Add the <a href="/list-of-update-sites">ImageJ-MATLAB</a> update site. See <a href="/update-sites/following">Following an update site</a> for more detail. <ol> <li>You go to <span class="bc"><span>Help</span> › <span>Update…</span></span></li> <li>Once checking status is done, click <span class="app-button">Manage update sites</span></li> <li>Tick “ImageJ-MATLAB” and then <span class="app-button">Close</span> <img src="/media/manage-update-sites-imagej-matlab.png" alt="fig:Manage_update_sites_ImageJ_MATLAB.png" width="550" /></li> <li>And then click <span class="app-button">Apply changes</span> on ImageJ Updater</li> <li>This will literally update and replace the existing, non-functional <code class="language-plaintext highlighter-rouge">ImageJ.m</code> file in the scripts folder with the genuine one.</li> </ol> </li> <li>You will need to install your own licensed copy of <a href="http://www.mathworks.com/products/matlab/">MATLAB</a>. All that ImageJ-MATLAB provides is adapters for evaluating scripts written in ImageJ2 to <a href="/scripting/matlab">MATLAB</a>, and converters between ImageJ2 and <a href="/scripting/matlab">MATLAB</a> data structures.</li> <li>Prior to MATLAB R2017b, MATLAB ships with Java 7, but ImageJ2 requires Java 8. You will need to change your MATLAB installation to use Java 8, by following the instructions for your platform: <a href="http://www.mathworks.com/matlabcentral/answers/130359-how-do-i-change-the-java-virtual-machine-jvm-that-matlab-is-using-on-windows">Windows</a>, <a href="http://www.mathworks.com/matlabcentral/answers/103056-how-do-i-change-the-java-virtual-machine-jvm-that-matlab-is-using-for-mac-os">macOS</a> or <a href="http://www.mathworks.com/matlabcentral/answers/130360-how-do-i-change-the-java-virtual-machine-jvm-that-matlab-is-using-for-linux-os">Linux</a>. If you run MATLAB R2017b and later, you don’t need to do this, as it ships and uses Java 8 already. <ul> <li>The command <code class="language-plaintext highlighter-rouge">version -java</code> will return the current Java version used by MATLAB</li> </ul> </li> <li>If you’re new to <a href="/scripting/matlab">MATLAB</a>, first check out Mathworks’ <a href="http://www.mathworks.com/help/matlab/getting-started-with-matlab.html">getting started guide</a>.</li> <li>If you’re familiar with the <a href="/scripting/matlab">MATLAB</a> language but haven’t written .m scripts before, look through the <a href="http://www.mathworks.com/help/matlab/scripts.html">script examples</a>.</li> </ul> <h3 id="memory-issue">Memory issue</h3> <ul> <li>In order to handle large images, the default Java Heap Memory size assigned won’t be enough and you may get the error <code class="language-plaintext highlighter-rouge">java.lang.OutOfMemoryError: Java heap space</code>.</li> <li>From MATLAB R2010a onward, you can increase the Jave Heap Memory size from <span class="bc"><span>Preferences</span> › <span>General</span> › <span>Java Heap Memory</span></span>. <img src="/media/scripting/matlab-java-heap-memory.png" alt="" /></li> <li>However, the maximum value allowed in the Preferences can still be too small for your purpose. In that case, you can directly edit <code class="language-plaintext highlighter-rouge">matlab.prf</code> file in the folder specified by the <code>prefdir&lt;\\code&gt; MATLAB function (eg. `C:\Users\xxxxxxx\AppData\Roaming\MathWorks\MATLAB\R2018b`). Find the parameter `JavaMemHeapMax` in the file and increase the number that follows the capital I (in MB) to increase the maximum Java heap memory size. The change will be reflected by the Preferences as above.</code></li> </ul> <!-- --> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>JavaMemHeapMax=I25000 </code></pre></div></div> <ul> <li>Alternatively, this can also be done by creating a <code class="language-plaintext highlighter-rouge">java.opts</code> file in <a href="https://mathworks.com/help/matlab/matlab_env/matlab-startup-folder.html">the startup directory</a> and overriding the default memory settings (see <a href="https://mathworks.com/help/matlab/matlab_env/java-opts-file.html">this documentation</a> for more information). For instance, <code class="language-plaintext highlighter-rouge">-Xmx512m</code> in your <code class="language-plaintext highlighter-rouge">java.opts</code> file may be a good start point.</li> <li>For the common “Out of Memory” error, see <a href="/learn/troubleshooting#outofmemoryerror"> the Troubleshooting page</a>.</li> </ul> <h2 id="creating-matlab-scripts-inside-imagej2">Creating MATLAB scripts inside ImageJ2</h2> <p>Using the <a href="/scripting/script-editor">Script Editor</a> you will be able to select <a href="/scripting/matlab">MATLAB</a> from the <a href="/scripting/script-editor#choosing-a-language">language menu</a>. You can also install and run .m scripts via the <a href="/scripting#creating-scripts-and-using-refresh-scripts"> standard script plugin infrastructure</a>.</p> <p>Actually running a <a href="/scripting/matlab">MATLAB</a> script from ImageJ2 is effectively like calling <a href="http://www.mathworks.com/help/matlab/ref/eval.html"><code class="language-plaintext highlighter-rouge">eval</code></a> on the script’s contents. The script will be evaluated as such in a remote <a href="/scripting/matlab">MATLAB</a> instance (which will be launched automatically, if needed). Note that only scripts, not functions, can be evaluated in this way. See <a href="http://www.mathworks.com/help/matlab/learn_matlab/scripts-and-functions.html">the MATLAB documentation</a> for an explanation of these concepts.</p> <blockquote> <p>NB: Because of the similarity to <code class="language-plaintext highlighter-rouge">eval</code>, when you need to assign character vector or string with special characters, they need to be <strong>escaped</strong>. A typical example is the handling of file/folder paths in Windows. Thus,</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>s = 'C:\Users\xxxxx\Desktop' </code></pre></div> </div> <p>will end up in an error in MATLAB:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Warning: Escaped character '\U' is not valid. See 'doc sprintf' for supported special characters. scijava_script523255 = 's = 'C:' Error: Character vector is not terminated properly. </code></pre></div> </div> <p>You need to escape backslashes as below:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>s = 'C:\\Users\\xxxxx\\Desktop' s = 'C:\Users\xxxxx\Desktop' </code></pre></div> </div> <p>For more details, see documetation for <code class="language-plaintext highlighter-rouge">sprintf</code> and learn about Special Characters.</p> </blockquote> <p>Options for controlling the startup of <a href="/scripting/matlab">MATLAB</a>, or killing existing <a href="/scripting/matlab">MATLAB</a> processes (e.g. if hidden) can be accessed via: <span class="bc"><span>Edit</span> › <span>Options</span> › <span>MATLAB…</span></span></p> <p><img src="/media/scripting/matlab-options.png" alt="" /></p> <div class="notice" style="font-size: 2; background: #ffcccb; border-left: 10px solid #f57900"><div class="notice-icon"><img src="/media/icons/warning.png" /></div><div class="notice-content"><p>Because the script is being passed from ImageJ2 to a remote <a href="/scripting/matlab">MATLAB</a>, <a href="/scripting/matlab">MATLAB</a> will not have access to ImageJ2’s classpath. Objects can be passed as variables to <a href="/scripting/matlab">MATLAB</a> (e.g. by using <a href="/scripting/parameters">script parameters</a>) but only if they are valid <a href="/scripting/matlab">MATLAB</a> classes or specially handled classes.</p> <p>Scripts requiring direct access to classes from ImageJ2, or from the original <a href="/software/imagej">ImageJ</a> (without auto-conversion support) should <a href="#running-imagej2-within-matlab">launch ImageJ2 from within MATLAB</a>.</p> <p>E.g. a script using <a href="https://javadoc.scijava.org/ImageJ1/ij/IJ.html"><code class="language-plaintext highlighter-rouge">ij.IJ</code></a> commands needs to be run from within MATLAB (or you’ll need to add Java class paths to MATLAB search path before calling <code class="language-plaintext highlighter-rouge">ij.IJ</code> commands).</p> </div> </div> <p>For example, by default all <a href="http://matlabcontrol.googlecode.com/svn/javadocs/doc/matlabcontrol/extensions/MatlabNumericArray.html">MatlabNumericArrays</a> will be converted to matrices within <a href="/scripting/matlab">MATLAB</a>. We also support auto-conversion of ImageJ2 <code class="language-plaintext highlighter-rouge">Dataset</code> out of the box, which can be read in by scripts using <code class="language-plaintext highlighter-rouge">matrix</code> parameters:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#@ matrix data #@output net.imagej.Dataset rval #@output net.imagej.Dataset mask % Performs dilation with a 3x3 square, % operating on the active dataset % Outputs the dilated mask and the original image % with the mask applied. rval = uint8(data); % convert to uint8 rval = mat2gray(rval); % normalize data mask = im2bw(rval,0.5); % make logical mask se = strel('square',3); % create structure to use in dilation mask = imdilate(mask,se); % perform dilation on the mask rval(~mask) = 0; % subtract mask from original dataset </code></pre></div></div> <p>This script will take the active <code class="language-plaintext highlighter-rouge">Dataset</code>, set it as an array variable named “data” in <a href="/scripting/matlab">MATLAB</a>, and set the matrixSum output value to the sum of the first three dimensions of the dataset.</p> <h3 id="global-state">Global state</h3> <p>MATLAB retains state (e.g. declared variables) as commands are executed, and ImageJ2 makes no special effort to clean up after a script. So whether running internally or communicating externally with MATLAB, state will be available to and persist after script execution. Thus one can, for example, write scripts in ImageJ2 referencing variables declared in MATLAB, without actually initializing them in the script.</p> <h3 id="passing-objects">Passing Objects</h3> <p>The caveat to <a href="#global-state">global state</a> is that, when running ImageJ2 externally, ImageJ2 and MATLAB run in separate <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/vm/">JVMs</a>. As a result, most objects can not be passed between the two. This makes <code class="language-plaintext highlighter-rouge">Dataset</code>s (and arrays) the currency that is passed between these applications.</p> <h3 id="return-values">Return values</h3> <p>Most of the <a href="/scripting">scripting languages</a> supported by ImageJ2 have implicit return values. As mentioned above, ImageJ2 will only execute true scripts, which do not have return values (in the MATLAB functional sense). There is a similar concept in the <code class="language-plaintext highlighter-rouge">ans</code> variable, which automatically gets the return value of executed statements if they are not explicitly assigned. However, due to the <a href="#global-state">global nature</a> of the ImageJ-MATLAB script language, it is not necessarily clear if <code class="language-plaintext highlighter-rouge">ans</code> was set by the script or a pre-existing command. Thus the decision was made that ImageJ-MATLAB scripts will <strong>never</strong> implicitly return a value. Instead, <a href="/scripting/parameters">the <code class="language-plaintext highlighter-rouge">#@output</code> annotation</a> should always be used - even for <code class="language-plaintext highlighter-rouge">ans</code>, as shown here:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#@output double[] ans % This trivial script demonstrates the use of % the "ans" variable in the SciJava-MATLAB % script language. % Unassigned statements in MATLAB are automatically % assigned to "ans". However, these scripts will not % return "ans" unless it is explicitly requested as % an output parameter. 0 </code></pre></div></div> <h3 id="importing-classes">Importing classes</h3> <p>To reference Java classes from MATLAB you will need to import them. <br /></p> <div class="notice" style="font-size: 2; background: #e6e6e6; border-left: 10px solid #fadd8d"><div class="notice-icon"><img src="/media/icons/imagej2.png" /></div><div class="notice-content"><p>Unlike the original <a href="/software/imagej">ImageJ</a>, <a href="/software/imagej2">ImageJ2</a> (and therefore <a href="/software/fiji">Fiji</a>) does not automatically import any classes. Consequently, scripts written for ImageJ will not run in ImageJ2 without adding the proper imports. The rationale is that the auto-import feature is not safe. What if two classes of the same name live in two different packages? Or if a new class is introduced that makes formerly unique names ambiguous? All of a sudden, all of the scripts that reference the original class no longer work. In short: auto-imports are dangerously imprecise.</p> </div> </div> <p><br /></p> <p>When running ImageJ2 externally, <a href="/scripting/matlab">MATLAB</a> will not have ImageJ2 classes in its classpath - so they can not simply be imported. Although <a href="/scripting/matlab">MATLAB</a> does support <a href="http://www.mathworks.com/help/matlab/matlab_external/bringing-java-classes-and-methods-into-matlab-workspace.html">editing its classpath</a> this is NOT recommended, as the classes loaded by <a href="/scripting/matlab">MATLAB</a> will not be the same as those loaded in ImageJ2.</p> <p>Instead, you can <a href="#running-imagej2-within-matlab">launch ImageJ2 inside MATLAB</a> and have it take care of managing the class loading for you. <a href="/scripting/matlab">MATLAB</a> then supports the use of import statements to <a href="http://www.mathworks.com/help/matlab/matlab_external/bringing-java-classes-and-methods-into-matlab-workspace.html#f46341">simplify class names</a>.</p> <h2 id="running-imagej2-within-matlab">Running ImageJ2 within MATLAB</h2> <div class="notice" style="font-size: 2; background: #ebf2f8; border-left: 10px solid #67a1fe"><div class="notice-icon"><img src="/media/icons/info.svg" width="32" /></div><div class="notice-content"><p>MATLAB versions prior to R2017b need to be <a href="http://www.mathworks.com/matlabcentral/answers/130359-how-do-i-change-the-java-virtual-machine-jvm-that-matlab-is-using-on-windows">tweaked to use Java 8</a> instead of Java 7.</p> </div> </div> <p>The ImageJ-MATLAB update site provides an <code class="language-plaintext highlighter-rouge">ImageJ.m</code> script that will start up an ImageJ2 instance inside a running <a href="/scripting/matlab">MATLAB</a> application. Launching the script is the same as for <a href="/plugins/miji">Miji</a>:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>addpath '/Applications/Fiji.app/scripts' % Update for your ImageJ2 (or Fiji) installation as appropriate ImageJ; </code></pre></div></div> <p>Now, you should see a new ImageJ2 instance shows up as a window. <img src="/media/scripting/imagej-launched-from-matlab.png" width="400" /></p> <p>In your MATLAB base workspace, you’ll find a variable <code class="language-plaintext highlighter-rouge">IJM</code>, which is a <code class="language-plaintext highlighter-rouge">net.imagej.matlab.ImageJMATLABCommands</code> Java object. <code class="language-plaintext highlighter-rouge">IJM</code> offers a few useful methods as below:</p> <ul> <li> <p><code class="language-plaintext highlighter-rouge">IJM.getDataset()</code> creates a MATLAB matrix from the active ImageJ2 image using its window title as the variable name (incompatible characters like <code class="language-plaintext highlighter-rouge">.</code> will be escaped with <code class="language-plaintext highlighter-rouge">_</code>). The MATLAB matrix is in <code class="language-plaintext highlighter-rouge">double</code> type. Although the X and Y axes are swapped, the third to fifth dimensions of the data are preserved.</p> </li> <li> <p><code class="language-plaintext highlighter-rouge">IJM.getDatasetAs(name)</code> creates a MATLAB matrix from the active ImageJ2 image, and assigns it to the specified variable name <code class="language-plaintext highlighter-rouge">name</code>.</p> </li> <li> <p><code class="language-plaintext highlighter-rouge">IJM.show(name)</code> takes the MATLAB matrix with the specified name <code class="language-plaintext highlighter-rouge">name</code> and displays it as an image in ImageJ2. The image is always in 32bit type. The X and Y axes are swapped. The third to fifth dimensions of the original MATLAB matrix are all piled in the fifth dimension and interpreted as time frames.</p> </li> </ul> <p>In the MATLAB command window, you’ll see something like this:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>-------------------------------------------------------------- ImageJ-MATLAB 0.7.2: MATLAB to ImageJ Interface -------------------------------------------------------------- JVM&gt; Version: 1.8.0_144 JVM&gt; Total amount of memory: 370176 Kb JVM&gt; Amount of free memory: 122756 Kb -- Welcome to ImageJ-MATLAB -- ImageJ-MATLAB consists of an extensible set of commands for passing information between ImageJ and MATLAB. See the individual sections below for a list of available commands. For more information and examples see: https://imagej.net/scripting/matlab --- MATLAB Command Plugins --- -- ImageJ MATLAB commands -- Usage: IJM.[command] help - prints a brief description of available commands getDataset - creates a MATLAB matrix from the active ImageJ image getDatasetAs(name) - creates a MATLAB matrix from the active ImageJ image, and assigns it to the specified variable name show(name) - takes the MATLAB matrix with the specified name and displays it as an image -------------------------------------------------------------- Status&gt; ImageJ is running. -------------------------------------------------------------- </code></pre></div></div> <p>The startup process automatically injects the ImageJ2 classpath into the <a href="/scripting/matlab">MATLAB</a> classpath, merging the two. At this point, you’ll have a working ImageJ2 and can now <a href="#creating-matlab-scripts-inside-imagej">run MATLAB scripts as normal</a> with access to the full unified classpath.</p> <h3 id="useful-commands">Useful Commands</h3> <h4 id="show-a-matlab-array-i-as-an-image-in-imagej2">Show a MATLAB array <code class="language-plaintext highlighter-rouge">I</code> as an image in ImageJ2</h4> <ul> <li> <p>using <code class="language-plaintext highlighter-rouge">IJM</code></p> <div class="language-matlab highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">IJM</span><span class="o">.</span><span class="nb">show</span><span class="p">(</span><span class="s1">'I'</span><span class="p">);</span> </code></pre></div> </div> </li> <li> <p>using the original ImageJ’s <code class="language-plaintext highlighter-rouge">ImagePlus</code> (<a href="https://github.com/kouichi-c-nakamura/copytoImagePlus">see</a>)</p> <div class="language-matlab highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">imp</span> <span class="o">=</span> <span class="n">copytoImagePlus</span><span class="p">(</span><span class="n">I</span><span class="p">);</span> <span class="n">imp</span><span class="o">.</span><span class="nb">show</span><span class="p">();</span> </code></pre></div> </div> </li> <li> <p>using ImgLib2 <code class="language-plaintext highlighter-rouge">ArrayImg</code></p> <div class="language-matlab highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">img</span> <span class="o">=</span> <span class="n">copytoImg</span><span class="p">(</span><span class="n">I</span><span class="p">);</span> <span class="n">net</span><span class="o">.</span><span class="n">imglib2</span><span class="o">.</span><span class="n">img</span><span class="o">.</span><span class="nb">display</span><span class="o">.</span><span class="n">imagej</span><span class="o">.</span><span class="n">ImageJFunctions</span><span class="o">.</span><span class="nb">show</span><span class="p">(</span><span class="n">img</span><span class="p">);</span> </code></pre></div> </div> </li> </ul> <h4 id="retrieve-an-image-data-in-imagej-as-a-matlab-array-i">Retrieve an image data in ImageJ as a MATLAB array <code class="language-plaintext highlighter-rouge">I</code></h4> <ul> <li> <p>using <code class="language-plaintext highlighter-rouge">IJM</code> with the current window title as the variable name</p> <div class="language-matlab highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">IJM</span><span class="o">.</span><span class="n">getDataset</span><span class="p">();</span> </code></pre></div> </div> </li> <li> <p>using <code class="language-plaintext highlighter-rouge">IJM</code> with the variable name <code class="language-plaintext highlighter-rouge">I</code></p> <div class="language-matlab highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">IJM</span><span class="o">.</span><span class="n">getDatasetAs</span><span class="p">(</span><span class="s1">'I'</span><span class="p">);</span> </code></pre></div> </div> </li> <li> <p>from <a href="/libs/imglib2">ImgLib2</a> <code class="language-plaintext highlighter-rouge">Img</code></p> <div class="language-matlab highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">I</span> <span class="o">=</span> <span class="n">copytoMATLAB</span><span class="p">(</span><span class="n">img</span><span class="p">);</span> </code></pre></div> </div> </li> </ul> <h4 id="quit-the-imagej-instance-from-matlab-command-line">Quit the ImageJ instance from MATLAB command line</h4> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>```matlab ij.IJ.run("Quit",""); ``` </code></pre></div></div> <h3 id="known-issues">Known Issues</h3> <h4 id="common-issues">Common Issues</h4> <ul> <li>Although the first and second dimensions, respectively, of MATLAB array are treated as rows (Y)and columns (X) in MATLAB, the first and second dimensions of an array are treated as X and Y, respectively, in ImageJ2. The axes are transposed by <code class="language-plaintext highlighter-rouge">IJM.show(name)</code>, <code class="language-plaintext highlighter-rouge">IJM.getDataset()</code>, <code class="language-plaintext highlighter-rouge">IJM.getDatasetAs(name)</code>. (See discussion <a href="http://forum.imagej.net/t/ijm-show-in-imagej-matlab-does-not-support-multi-dimentional-or-multi-channel-images/10156">here for details</a>)</li> <li><code class="language-plaintext highlighter-rouge">IJM</code> object is always added to the MATLAB base workspace. In order to access <code class="language-plaintext highlighter-rouge">IJM</code> methods from within a MATLAB function, you’ll need to use <code class="language-plaintext highlighter-rouge">assignin</code>, <code class="language-plaintext highlighter-rouge">evalin</code>, and/or <code class="language-plaintext highlighter-rouge">global</code> (see <a href="http://forum.imagej.net/t/how-can-i-use-imagej-from-within-a-matlab-function/10297">here for details</a>)</li> <li> <p>When launching ImageJ2, you’ll see the error message below:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>log4j:WARN No appenders could be found for logger (loci.formats.ClassList). log4j:WARN Please initialize the log4j system properly. </code></pre></div> </div> </li> </ul> <h4 id="issues-specific-to-ijmshowname">Issues specific to <code class="language-plaintext highlighter-rouge">IJM.show(name)</code></h4> <ul> <li>A multi (&gt;2) dimensional image (with multi-channels, slices or time frames) is treated as single channel images with multiple time frames irrespective of the dimensions. For example, if a MATLAB array <code class="language-plaintext highlighter-rouge">A</code> has the size <code class="language-plaintext highlighter-rouge">[i,j,k]</code>, where <code class="language-plaintext highlighter-rouge">i</code>, <code class="language-plaintext highlighter-rouge">j</code>, and <code class="language-plaintext highlighter-rouge">k</code> correspond to the sizes in rows, columns and channels, then <code class="language-plaintext highlighter-rouge">IJM.show(name)</code> will show it as <code class="language-plaintext highlighter-rouge">[j,i,1,1,k]</code>, where X and Y axes are flipped over (see below) and channels are interpreted as frames. On the other hand, <code class="language-plaintext highlighter-rouge">IJM.getDataset()</code> and <code class="language-plaintext highlighter-rouge">IJM.getDatasetAs(name)</code> can maintain the dimensions of the image data (except the flipping over of X and Y axes). <ul> <li>From GUI, you can fix this by <span class="bc"><span>Image</span> › <span>Hyperstacks</span> › <span>Re-order Hyperstack…</span></span></li> <li>For a scripting-based solution, you can run <code class="language-plaintext highlighter-rouge">ij.IJ.run("Re-order Hyperstack ...", "channels=[Frames (t)] slices=[Slices (z)] frames=[Channels (c)]");</code> in MATLAB for the example above to have dimensions <code class="language-plaintext highlighter-rouge">[i,j,k,1,1]</code> in XYCZT format. See <a href="http://forum.imagej.net/t/imagej-matlab-ijm-show-does-not-reliably-pass-numeric-data-to-imagej/10724">here for details</a>.</li> <li>When you have multiple channels, slices and frames, they’re all damped into the fifth dimension, so it takes extra effort to correct the data format.</li> </ul> </li> <li>Also, data is always handled as 32bit (int32) per channel. <strong>Conversion to 8 bit or 16 bit images require an extra care</strong> to avoid unwanted scaling of numeric values (see <a href="http://forum.imagej.net/t/imagej-matlab-ijm-show-does-not-reliably-pass-numeric-data-to-imagej/10724">here for details</a>)</li> </ul> <h4 id="solution-to-ijmshowname-issues">Solution to <code class="language-plaintext highlighter-rouge">IJM.show(name)</code> issues</h4> <ul> <li>Until someone can fix <code class="language-plaintext highlighter-rouge">IJM.show(name)</code> properly, consider using <a href="https://github.com/kouichi-c-nakamura/copytoImagePlus"><code class="language-plaintext highlighter-rouge">copytoImagePlus</code></a> MATLAB function instead. It solves all the issues of <code class="language-plaintext highlighter-rouge">IJM.show(name)</code> descrived above and create an ImagePlus hyperstack. See the example of a 5D Hyperstack below.</li> <li><code class="language-plaintext highlighter-rouge">copytoImg</code> and <code class="language-plaintext highlighter-rouge">copytoImgPlus</code> MATLAB functions are also available from <code class="language-plaintext highlighter-rouge">Fiji.app/scripts</code> folder to allow you convert a MATLAB array to ImageJ2 (ImgLib2) <code class="language-plaintext highlighter-rouge">Img</code> and <code class="language-plaintext highlighter-rouge">ImgPlus</code> objects, respectively. See also <a href="/libs/imglib2/matlab">this page</a>.</li> </ul> <h3 id="examples">Examples</h3> <h4 id="accessing-imagej-java-api-from-matlab">Accessing ImageJ Java API from MATLAB</h4> <p>The following Java commands work in MATLAB command window to open a sample image in ImageJ.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>imp = ij.IJ.openImage("http://imagej.net/ij/images/boats.gif"); imp.show() </code></pre></div></div> <p><img src="/media/scripting/boats-screenshot.png" width="400" /></p> <h4 id="opening-a-matlab-array-as-an-image-in-imagej2">Opening a MATLAB array as an image in ImageJ2</h4> <p>The following will open a MATLAB array data as an image in ImageJ2. Note that the X and Y of the image are transposed in ImageJ2 in the second image (see Limitations of <code class="language-plaintext highlighter-rouge">IJM.show()</code> above).</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>corn_gray = imread('corn.tif',3); imshow(corn_gray); % show in MATLAB IJM.show('corn_gray'); % show in ImageJ2 corn_gray_tp = corn_gray'; % transpose array IJM.show('corn_gray_tp'); % show in ImageJ2 properly </code></pre></div></div> <p><img src="/media/scripting/corn-gray-matlab.png" width="250" /> <img src="/media/scripting/corn-gray.png" width="250" /> <img src="/media/scripting/corn-gray-transposed.png" width="250" /></p> <p>A more general solution to this issue of X-Y transposition can be achieved by <code class="language-plaintext highlighter-rouge">permute</code> function of MATLAB. But please beware of <a href="https://stackoverflow.com/questions/36065182/why-does-matlabs-permute-not-need-extra-memory">memory demands by <code class="language-plaintext highlighter-rouge">permute</code></a>.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>% I is a MATLAB array with 2 or more dimensions order = [{2, 1}, num2cell(3:ndims(I))]; I2 = permute(I,[order{:}]); IJM.show(I2) % this will show an image in proper X and Y </code></pre></div></div> <p>If you wish to show an image with ImageJ from within a MATLAB function, you can achieve that by using <a href="https://github.com/kouichi-c-nakamura/copytoImagePlus"><code class="language-plaintext highlighter-rouge">copytoImagePlus</code></a> as below:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>function demofun() corn_gray = imread('corn.tif',3); imp = copytoImagePlus(corn_gray); imp.show(); end </code></pre></div></div> <h4 id="retrieving-a-matlab-array-data-from-an-image-in-imagej2">Retrieving a MATLAB array data from an image in ImageJ2</h4> <p>Additionally, the <a href="https://github.com/scijava/scripting-matlab/">Scripting-MATLAB</a> library also includes an extensible <a href="/scripting/matlab">MATLAB</a> command framework. This allows for the creation of utility classes that will be automatically populated into <a href="/scripting/matlab">MATLAB</a> variables for easy access. For example, you could use ImageJ2 to open a dataset and perform thresholding (or any other processing steps), then in <a href="/scripting/matlab">MATLAB</a> use the <code class="language-plaintext highlighter-rouge">IJM.getDatasetAs(name)</code> command to set the active dataset as a <a href="/scripting/matlab">MATLAB</a> matrix variable with a specified name, for further analysis.</p> <p>For example, instead of using a script as <a href="#creating-matlab-scripts-inside-imagej">described above</a>, we could achieve the same result by executing the following commands in the MATLAB prompt:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>IJM.getDatasetAs('data') % import the image as a MATLAB matrix rval = uint8(data); % convert to uint8 rval = mat2gray(rval); % normalize data mask = im2bw(rval,0.5); % make logical mask se = strel('square',3); % create structure to use in dilation mask = imdilate(mask,se); % perform dilation on the mask rval(~mask) = 0; % subtract mask from original dataset IJM.show('rval') % display the rval array in ImageJ2 IJM.show('mask') % display the mask array in ImageJ2 </code></pre></div></div> <p>If you wish to assign the numeric data of an ImageJ2 image into the Workspace of a MATLAB function, rather than the base Workspace, you can achieve that by making the variable global as below:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>function demofun() evalin('base','global data'); evalin('base','IJM.getDatasetAs(''data'')'); global data % data is now available in the Workspace of this function disp(size(data)) clear global data % leaving a global varibale can cause unexpected problems end </code></pre></div></div> <h4 id="opening-a-2d-5d-matlab-array-as-a-hyperstack-in-imagej">Opening a 2D-5D MATLAB array as a Hyperstack in ImageJ</h4> <p>If you use <a href="https://github.com/kouichi-c-nakamura/copytoImagePlus"><code class="language-plaintext highlighter-rouge">copytoImagePlus</code></a>, you can open a 2D-5D MATLAB array as a Hyperstack with the right image type (8bit, 16bit, and 32bit for uint8, uint16, and other types of MATLAB array, respectively) in ImageJ with a simple syntax. Also, you can use <code class="language-plaintext highlighter-rouge">copytoImagePlus</code> within a MATLAB function independently of the <code class="language-plaintext highlighter-rouge">IJM</code> variable in base workspace.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>imp = ij.IJ.openImage("http://imagej.net/ij/images/Spindly-GFP.zip"); imp.show(); IJM.getDatasetAs('I'); % I is 5-D double with the size of 171x196x2x5x51 in MATLAB I16 = uint16(I); IJM.show('I16') % ImagePlus, 32-bit, 171x196x1x1x510 imp4 = copytoImagePlus(I16,'XYCZT') % ImagePlus, 16-bit, 171x196x2x5x51 imp4.show(); </code></pre></div></div> <p><code class="language-plaintext highlighter-rouge">IJM.show(name)</code> cannot reproduce the dimensions and the data type. Channels (C), slices (Z), and frames (T) are all treated as frames. Image type is 32 bit. <img src="/media/scripting/ijmshow-image006.png" width="150" /></p> <p><code class="language-plaintext highlighter-rouge">copytoImagePlus</code> can keep the dimensions and the data type. <img src="/media/scripting/image003.png" width="150" /></p> <h1 id="source">Source</h1> <ul> <li><a href="https://github.com/scijava/scripting-matlab/">Scripting-MATLAB</a> provides the script engine plugins.</li> <li><a href="https://github.com/imagej/imagej-matlab/">ImageJ-MATLAB</a> defines the ImageJ2-specific translators.</li> </ul> <h1 id="video-presentation">Video presentation</h1> <div class="video"><figure class="figure"><iframe src="https://player.vimeo.com/video/140929686?color=b80013&amp;title=0&amp;byline=0&amp;portrait=0" width="400" height="225" frameborder="0" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen="" allow="accelerometer; encrypted-media; gyroscope; picture-in-picture"></iframe><div class="linkback" style="width: 400px"><a href="https://vimeo.com/140929686">[view on Vimeo]</a></div><figcaption><p>ImageJ+MATLAB: Reunited</p> </figcaption>&lt;/div&gt; # Publication <span class="citation" data-citation-id="10.1093/bioinformatics/btw681" data-citation-style="apa"><a href="https://doi.org/10.1093/bioinformatics/btw681">doi:10.1093/bioinformatics/btw681</a></span> # See also - [Creating Imglib2 images in MATLAB](/libs/imglib2/matlab) - [Using TrackMate from MATLAB](/plugins/trackmate/using-from-matlab) - [Analyzing TrackMate results with MATLAB](/plugins/trackmate/analyzing-results-with-matlab) - [Comparison of Matlab functions and Ops](/libs/imagej-ops/comparison-with-matlab) - [Miji](/plugins/miji) (legacy) </figure></div> </div> </div> </section> <!-- Footer --> <section id="footer"> <ul class="icons"> <li><a href="https://forum.image.sc/tag/imagej" class="icon alt"><img src="/media/icons/image-sc.png" width="24" style="vertical-align: middle; margin-top: -10px"><span class="label">Forum</span></a></li> <li><a href="https://twitter.com/hashtag/ImageJ" class="icon brands alt fa-twitter"><span class="label">Twitter</span></a></li> <li><a href="https://github.com/imagej" class="icon brands alt fa-github"><span class="label">GitHub</span></a></li> </ul> <ul class="copyright"> <li>Design: <a href="http://html5up.net">HTML5 UP</a></li> </ul> </section> <!-- Hamburger menu --> <nav role="navigation"> <div id="ham-toggle"> <input type="checkbox" /> <span></span> <!-- top bun --> <span></span> <!-- hamburger --> <span></span> <!-- bottom bun --> <ul id="ham-menu"> <div class="menu"><h3>ImageJ Docs</h3> <ul> <li><a href="/downloads">Download</a></li> <li><details><summary>Learn</summary><ul> <li><details><summary><a href="/learn">ImageJ Basics</a></summary><ul> <li><a href="/tutorials">Tutorials</a></li> <li><a href="/learn/user-guides">User Guides</a></li> <li><a href="/learn/keyboard-shortcuts">Keyboard Shortcuts</a></li> <li><a href="/learn/tips-and-tricks">Tips and Tricks</a></li> <li><a href="/learn/troubleshooting">Troubleshooting</a></li> <li><a href="/learn/faq">Frequently Asked Questions</a></li> <li><details><summary><a href="/platforms">Supported Platforms</a></summary><ul> <li><a href="/platforms/windows">Windows</a></li> <li><a href="/platforms/macos">MacOS</a></li> <li><a href="/platforms/linux">Linux</a></li> <li><a href="/platforms/pi">Raspberry Pi</a></li> <li><a href="/platforms/android">Android</a></li> </ul></details></li> <!-- Learn/ImageJ Basics/Supported Platforms --> <li><details><summary><a href="/formats">File Formats</a></summary><ul> <li><a href="/formats/bio-formats">Bio-Formats</a></li> <li><a href="/formats/video">Video formats</a></li> <li><a href="/formats/quicktime">QuickTime</a></li> <li><a href="/formats/tiff">TIFF</a></li> <li><a href="/formats/pdf">PDF</a></li> <li><a href="/formats/dicom">DICOM</a></li> <li><a href="/formats/lsm">Zeiss LSM</a></li> <li><a href="/formats/olympus">Olympus VSI</a></li> <li><a href="/formats/lurawave">Opera Flex</a></li> <li><a href="/formats/fib-sem">FIB-SEM</a></li> </ul></details></li> <!-- Learn/ImageJ Basics/File Formats --> </ul></details></li> <!-- Learn/ImageJ --> <li><details><summary><a href="/imaging">Scientific Imaging</a></summary><ul> <li><a href="/imaging/principles">Principles of Scientific Imaging</a></li> <li><a href="/imaging/annotating-images">Annotating Images</a></li> <li><a href="/imaging/colocalization-analysis">Colocalization</a></li> <li><a href="/imaging/color-image-processing">Color Image Processing</a></li> <li><a href="/imaging/deconvolution">Deconvolution</a></li> <li><a href="/imaging/image-intensity-processing">Image Intensity Processing</a></li> <li><a href="/imaging/particle-analysis">Particle Analysis</a></li> <li><a href="/imaging/registration">Registration</a></li> <li><a href="/imaging/segmentation">Segmentation</a></li> <li><a href="/imaging/stack-slice-manipulations">Stack-slice Manipulations</a></li> <li><a href="/imaging/t-functions">T-functions</a></li> <li><a href="/imaging/tracking">Tracking</a></li> <li><a href="/imaging/visualization">Visualization</a></li> <li><a href="/imaging/z-functions">Z-functions</a></li> </ul></details></li> <!-- Learn/Scientific Imaging --> </ul></details></li> <!-- Learn --> <li><details open><summary><a href="/plugins">Extend</a></summary><ul> <li><a href="/list-of-extensions">List of Extensions</a></li> <li><details><summary><a href="/update-sites">Update Sites</a></summary><ul> <li><a href="/list-of-update-sites">List of Update Sites</a></li> <li><a href="/update-sites/following">Following an Update Site</a></li> <li><a href="/update-sites/setup">Creating an Update Site</a></li> <li><a href="/update-sites/tos">Terms of Service</a></li> <li><a href="/update-sites/automatic-uploads">Automatic Upload</a></li> <li><a href="/update-sites/core-uploads">Uploading to Core Sites</a></li> <li><a href="/update-sites/faq">Update Sites FAQ</a></li> <li><a href="/update-sites/stats">Statistics</a></li> </ul></details></li> <!-- Extend/Update Sites --> <li><details open><summary><a href="/scripting">Scripting</a></summary><ul> <li><a href="/scripting/basics">Scripting Basics</a></li> <li><a href="/scripting/script-editor">Script Editor</a></li> <li><a href="/scripting/parameters">Parameters</a></li> <li><a href="/scripting/user-input">User Input</a></li> <li><a href="/scripting/auto-imports">Auto Import</a></li> <li><a href="/scripting/templates">Templates</a></li> <li><a href="/scripting/batch">Batch Processing</a></li> <li><a href="/scripting/headless">Running Headlessly</a></li> <li><a href="/scripting/comparisons">Scripting Comparisons</a></li> <li><a href="/scripting/toolbox">Toolbox</a></li> <li><details open><summary>Languages</summary><ul> <li><a href="/scripting/beanshell">BeanShell Scripting</a></li> <li><a href="/scripting/groovy">Groovy Scripting</a></li> <li><a href="/scripting/macro">ImageJ Macro</a></li> <li><a href="/scripting/javascript">JavaScript</a></li> <li><a href="/scripting/clojure">Lisp (Clojure)</a></li> <li><a class="current-page">MATLAB</a></li> <li><a href="/scripting/jython">Python (Jython)</a></li> <li><a href="/scripting/python">Python (native)</a></li> <li><a href="/scripting/renjin">R (Renjin)</a></li> <li><a href="/scripting/jruby">Ruby (JRuby)</a></li> <li><a href="/scripting/scala">Scala Scripting</a></li> </ul></details></li> <!-- Extend/Scripting/Languages --> </ul></details></li> <!-- Extend/Scripting --> <li><details><summary><a href="/develop">Development</a></summary><ul> <li><a href="/develop/philosophy">Philosophy</a></li> <li><a href="/develop/architecture">Architecture</a></li> <li><a href="/develop/source">Source code</a></li> <li><a href="/develop/project-management">Project management</a></li> <li><a href="/develop/coding-style">Coding style</a></li> <li><a href="/develop/javadoc">Using Javadoc</a></li> <li><a href="/develop/debugging">Debugging</a></li> <li><a href="/develop/wish-list">Wish list</a></li> <li><details><summary>Tools</summary><ul> <li><a href="/develop/github">GitHub</a></li> <li><a href="/develop/maven">Maven</a></li> <li><a href="/develop/ci">CI/CD</a></li> <li><a href="/develop/dotfiles">Dotfiles</a></li> <li><details><summary><a href="/develop/ides">IDEs</a></summary><ul> <li><a href="/develop/eclipse">Eclipse</a></li> <li><a href="/develop/netbeans">NetBeans</a></li> <li><a href="/develop/intellij">IntelliJ IDEA</a></li> <li><a href="/develop/command-line">Command Line</a></li> </ul></details></li> </ul></details></li> <!-- Extend/Development/Tools --> <li><details><summary>Guides</summary><ul> <li><a href="/develop/plugins">Writing plugins</a></li> <li><a href="/develop/improving-the-code">Contributing to a plugin</a></li> <li><a href="/develop/releasing">Development lifecycle</a></li> <li><a href="/develop/building-a-pom">Building a POM</a></li> <li><a href="/develop/debugging-exercises">Hands-on debugging</a></li> <li><a href="/develop/writing-ops">Adding new ops</a></li> <li><a href="/develop/formats">Adding new formats</a></li> <li><a href="/develop/native-libraries">Using native libraries</a></li> <li><a href="/develop/tips">Tips for developers</a></li> <li><a href="/develop/cpp-tips">Tips for C++ developers</a></li> <li><a href="/develop/ij1-plugins">ImageJ 1.x plugins</a></li> <li><a href="/develop/versioning">Versioning</a></li> <li><a href="/develop/logging">Logging</a></li> <li><a href="/develop/uber-jars">Uber-JARs</a></li> </ul></details></li> <!-- Extend/Development/Guides --> <li><details><summary><a href="/develop/git">Git</a></summary><ul> <li><a href="/develop/git/eclipse">Git in Eclipse (EGit)</a></li> <li><a href="/develop/git/mini-howto">Git mini howto</a></li> <li><a href="/develop/git/workshop">Git workshop</a></li> <li><a href="/develop/git/conflicts">Git conflicts</a></li> <li><a href="/develop/git/topic-branches">Git topic branches</a></li> <li><a href="/develop/git/notes">Git notes</a></li> <li><a href="/develop/git/reflogs">Git reflogs</a></li> <li><a href="/develop/git/submodules">Git submodules</a></li> <li><a href="/develop/git/pinpoint-regressions">How to pinpoint regressions</a></li> <li><a href="/develop/git/publish-a-repository">How to publish a git repository</a></li> <li><a href="/develop/git/extract-a-subproject">How to extract a subproject</a></li> </ul></details></li> <!-- Extend/Development/Git --> </ul></details></li> <!-- Extend/Development --> </ul></details></li> <!-- Extend --> <li><details><summary><a href="/contribute">Contribute</a></summary><ul> <li><a href="/contribute/citing">Citing</a></li> <li><a href="/people">Contributors</a></li> <li><a href="/orgs">Organizations</a></li> <li><a href="/contribute/governance">Governance</a></li> <li><a href="/contribute/funding">Funding</a></li> <li><a href="/contribute/fiji">Contributing to Fiji</a></li> <li><details><summary><a href="/licensing">Licensing</a></summary><ul> <li><details><summary><a href="/licensing/open-source">Open Source</a></summary><ul> <li><a href="/licensing/apache">Apache</a></li> <li><a href="/licensing/bsd">BSD</a></li> <li><a href="/licensing/epl">EPL</a></li> <li><a href="/licensing/gpl">GPL</a></li> <li><a href="/licensing/lgpl">LGPL</a></li> <li><a href="/licensing/mit">MIT</a></li> <li><a href="/licensing/public-domain">Public domain</a></li> <li><a href="/licensing/big">BIG</a></li> </ul></details></li> <!-- Contribute/Licensing/Open Source --> <li><a href="/licensing/closed-source">Proprietary</a></li> </ul></details></li> <!-- Contribute/Licensing --> <li><details><summary><a href="/editing">Editing the Wiki</a></summary><ul> <li><a href="/editing/advanced">Advanced Editing</a></li> <li><a href="/editing/buttons">Buttons</a></li> <li><a href="/editing/citations">Citations</a></li> <li><a href="/editing/code">Source Code</a></li> <li><a href="/editing/debugging">Debugging</a></li> <li><a href="/editing/headers">Headers</a></li> <li><a href="/editing/icons">Icons</a></li> <li><a href="/editing/images">Images</a></li> <li><a href="/editing/keys">Keyboard Shortcuts</a></li> <li><a href="/editing/linking">Linking</a></li> <li><a href="/editing/math">Math Expressions</a></li> <li><a href="/editing/menu-paths">Menu Paths</a></li> <li><a href="/editing/navigation">Navigation</a></li> <li><a href="/editing/notices">Notices</a></li> <li><a href="/editing/people">People</a></li> <li><a href="/editing/pitfalls">Pitfalls</a></li> <li><a href="/editing/quizzes">Quizzes</a></li> <li><a href="/editing/statbox">Statbox</a></li> <li><a href="/editing/symbols">Symbols</a></li> <li><a href="/editing/tables">Tables</a></li> <li><a href="/editing/timelines">Timelines</a></li> <li><a href="/editing/tooltips">Tooltips</a></li> <li><a href="/editing/videos">Videos</a></li> <li><a href="/editing/whitespace">Whitespace</a></li> </ul></details></li> <!-- Contribute/Editing the Wiki --> </ul></details></li> <!-- Contribute --> <li><details><summary><a href="/discuss">Discuss</a></summary><ul> <li><a href="/discuss/bugs">Reporting Issues</a></li> <li><a href="/discuss/mailing-lists">Mailing Lists</a></li> <li><a href="/discuss/chat">Chat</a></li> </ul></details></li> <!-- Discuss --> <li><details><summary>Explore</summary><ul> <li><a href="/news">News</a></li> <li><details><summary><a href="/events">Events</a></summary><ul> <li><a href="/events/presentations">Presentations</a></li> <li><a href="/events/conferences">Conferences</a></li> <li><a href="/events/hackathons">Hackathons</a></li> <li><a href="/events">More...</a></li> </ul></details></li> <!-- Discuss/Events --> <li><details><summary><a href="/libs">Libraries</a></summary><ul> <li><a href="/libs/imagej-ops">ImageJ Ops</a></li> <li><a href="/libs/imagej-common">ImageJ Common</a></li> <li><a href="/libs/imagej-legacy">ImageJ Legacy</a></li> <li><a href="/libs/scijava">SciJava</a></li> <li><a href="/libs/scifio">SCIFIO</a></li> <li><details><summary><a href="/libs/imglib2">ImgLib2</a></summary><ul> <li><a href="/libs/imglib2/getting-started">Getting Started</a></li> <li><a href="/libs/imglib2/accessors">Accessors</a></li> <li><a href="/libs/imglib2/accessibles">Accessibles</a></li> <li><a href="/libs/imglib2/examples">Examples</a></li> <li><a href="/libs/imglib2/workshop-introductory">Introductory Workshop</a></li> <li><a href="/libs/imglib2/workshop-advanced">Advanced Workshop</a></li> <li><a href="/libs/imglib2/matlab">ImgLib2 images in MATLAB</a></li> <li><a href="/libs/imglib2/benchmarks">Benchmarks</a></li> <li><a href="/libs/imglib2/faq">FAQ</a></li> <li><a href="/libs/imglib2/developing">Developing ImgLib2</a></li> <li><a href="/libs/imglib2/discussion">ImgLib2 Discussion</a></li> </ul></details></li> <!-- Explore/Libraries/ImgLib2 --> <li><a href="/libs">More...</a></li> </ul></details></li> <!-- Explore/Libraries --> <li><details><summary><a href="/software">Software</a></summary><ul> <li><a href="/software/nih-image">NIH Image</a></li> <li><a href="/software/imagej">ImageJ</a></li> <li><a href="/software/imagej2">ImageJ2</a></li> <li><a href="/software/fiji">Fiji</a></li> <li><a href="/software">More...</a></li> </ul></details></li> <!-- Explore/Software --> </ul></details></li> <!-- Explore --> </ul> </div> </ul> </div> </nav> <div id="dock-overlay-left" class="dock-overlay" data-dock-target="left-column"></div> <div id="dock-overlay-right" class="dock-overlay" data-dock-target="right-column"></div> <!-- Search results --> <div id="search-results"> <div id="search-hits"></div> <div id="pagination"></div> </div> <!-- Scripts --> <!-- Libraries --> <script src="/assets/js/jquery.min.js"></script> <script src="/assets/js/jquery.scrolly.min.js"></script> <script src="/assets/js/browser.min.js"></script> <script src="/assets/js/breakpoints.min.js"></script> <script src="/assets/js/util.js"></script> <script src="/assets/js/jquery.toc.js"></script> <script src="/assets/js/lightbox.min.js"></script> <!-- Extensions --> <!-- Site code --> <script src="/assets/js/main.js"></script> <script src="/assets/js/dock.js"></script> <script src="/assets/js/code.js"></script> <!-- Anchors --> <script src="https://cdn.jsdelivr.net/npm/anchor-js/anchor.min.js"></script> <script>anchors.add('#page h1, #page h2, #page h3, #page h4, #page h5, #page h6');</script> <!-- Citations --> <script src="https://cdn.jsdelivr.net/npm/citation-js@0.4.0-9"></script> <script src="/assets/js/cite.js"></script> <!-- Search --> <script src="https://cdn.jsdelivr.net/npm/algoliasearch@4.13.0/dist/algoliasearch-lite.umd.js"></script> <script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4.40.3/dist/instantsearch.production.min.js"></script> <script src="/assets/js/search.js"></script> <script src="/assets/js/search-tweaks.js"></script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10