Developer Zone

Advanced Software Development with MATLAB

Open and extensible

MATLAB is an open and extensible platform unique in its ability to dovetail with best-in-class technologies. Open, refers to the fact that much of MATLAB functionality is shipped in MATLAB files whose source can be studied. The community driven contributions on File Exchange has grown over the years to a scale that rivals the code that ships on the DVD. Extensible, refers to MATLAB's ability to integrate with other languages and technology stacks.

MATLAB's extensibility provides access to a wide variety of functionality in C/C++, Java, .NET, Perl, Python, etc. Some 3rd party libraries such as Xerces™ (for processing XML) are baked into the shipping MATLAB product itself.

MATLAB is designed to be extensible, for example, the MEX interface provides access to nearly all of FORTRAN and C/C++ functionality for the expression of numeric algorithms.

Leveraging the extensibility of MATLAB - the Triple-word-score

How does MATLAB's extensibility help you in your day-to-day life?

An immediate problem and answer to this question presented itself in the authoring of content for this very blog. You may have noticed the "published in MATLAB" at the very bottom of this blog post.

Bloggers on this site often use MATLAB for authoring their content and I am no exception. A spellchecker is always helpful when writing. Since I use the MATLAB editor, there is an immediate need to simplify my workflow by providing easy access to spellcheck from the MATLAB environment itself.

A spellchecker could benefit other users so I quickly drew up a list of requirements. A MATLAB spellchecker needed to be:

  • lightweight and free (i.e. be accessible across most versions of MATLAB)
  • cross-platform (i.e. work with our Windows, Mac and Linux releases)
  • multi-lingual (i.e. make no assumptions around use of English as we have over a million users worldwide)
  • customizable (i.e. allow for the modification of word lists)
  • easy-to-build-and-use (i.e. not turn into a huge project in itself)

The first step was to ensure that I was not re-inventing the wheel. A quick File Exchange search and follow-up internet searches turned up nothing.

Not finding what I needed, I decided to build a spellchecker by leveraging existing technology with a little MATLAB effort. I call extending MATLAB functionality a "triple-word-score" since it nets large increments in productivity with minimal effort.

Putting it together by NOT re-inventing the wheel

Given the cross-platform requirement, I quickly eliminated the .NET options and settled for a Java library with a clean API. Jazzy had an aging codebase but a clean API and a full set of language dictionaries which fit my requirements well. It allowed me to put a check against the cross-platform and lightweight and free requirements.

The Jazzy API lends itself to integration through a simple wrapper in Java which allows for a cleaner calling interface. To build our Java class (, I fired up a Java IDE and imported the Jazzy libraries from Sourceforge:

/* Package Specification*/
package com.mathworks.spellcheck;

/* Jazzy Imports */
import com.swabunga.spell.engine.SpellDictionary;
import com.swabunga.spell.engine.SpellDictionaryHashMap;
import com.swabunga.spell.event.SpellCheckEvent;
import com.swabunga.spell.event.SpellCheckListener;
import com.swabunga.spell.event.SpellChecker;
import com.swabunga.spell.event.StringWordTokenizer;

/* Java Imports */
import java.util.Iterator;
import java.util.List;

Building a simple java class that implements a listener for our events.

 * This class provides a MATLAB callback to check the spelling of the comments
 * in the code.
public class SpellCheck implements SpellCheckListener {

    private SpellChecker spellCheck; 
    /* Method that sets the dictionary */ 
    public void setDictionary(String dictFile) {
        try {
            SpellDictionary dictionary = new SpellDictionaryHashMap(new File(dictFile));
            spellCheck = new SpellChecker(dictionary);
        } catch (Exception e) {
    /* Method to check the spelling */ 
    public void checkSpelling(String inputText) {
        try {
            spellCheck.checkSpelling(new StringWordTokenizer(inputText));
        } catch (Exception e) {
    /* Event listener goes here */

We have a method to set a language specific dictionary and a method to check the spelling of an input String. The Jazzy library will then fire an event that we act on. The event listener looks like this:

   /* Event listener that populates the suggestions and echo them as necessary */
    public void spellingError(SpellCheckEvent event) {
        String message = new String("MISSPELT WORD: " + event.getInvalidWord()); 
        /* Echo to the command prompt */ 
        List suggestions = event.getSuggestions();
        if (suggestions.size() > 0) {
            for (Iterator suggestedWord = suggestions.iterator(); suggestedWord.hasNext();) {
                System.out.println("\tSuggested Word: " +;
        } else {
            System.out.println("\tNo suggestions");

The first version of this Java module just echoes the spelling error and suggestions to the Command Window. Compiling the java code and packaging it into a JAR file is usually a single click operation in most Java development tools.

Building jar: I:\Work\SpellChecker\lib\java\MATLABSpellCheck\dist\MATLABSpellCheck.jar
BUILD SUCCESSFUL (total time: 1 second)

MATLAB is built on a Java platform so enabling this module was a single line of code to add it to the dynamic java classpath. I had my basic spellcheck in MATLAB in minutes.


Testing it out

To test it out, I downloaded a few sample dictionaries from the JazzyDicts SourceForge repository which contains a wide selection of supported languages. Then, in MATLAB:

% Make Jazzy available to MATLAB
import com.mathworks.spellcheck.*;

% Setup a default language.
dictFile = which('en_USx.dic');

% Create a jazzy spellchecker
obj = SpellCheck();

% Check the spelling of an input string
obj.checkSpelling('Yello Worrld')
	Suggested Word: Jello
	Suggested Word: Cello
	Suggested Word: cello
	Suggested Word: hello
	Suggested Word: world

It only takes a few more lines of code to wrap this as an easy-to-use MATLAB class that inspects the contents of my editor.

With that I was able to check-off the requirements for being multi-lingual and easy-to-use. I added common words like MATLAB, etc. to my local dictionary file and continue improving it as I go.

The module itself is less important than the technique that shows one way to extend the MATLAB platform. I decided to keep this post a single, simple example to describe MATLAB's ability to leverage other technologies out there. We plan to talk about how to produce an user-friendly MATLAB interface in the upcoming posts.

In summary, MATLAB's functionality can be complemented and strengthened with 3rd party technologies to provide a single cohesive user experience.

MATLAB can also bring powerful functionality into other platforms, but that is a whole other topic.

Published with MATLAB® R2015b

  • print
  • send email


要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。