Matlab DDE support

Windows DDE was a 1990’s technology used to transfer control between Windows applications. DDE was fully supported and documented in old Matlab releases (R13, R14). DDE is no longer documented in Matlab, although it apparently still works. In fact, Matlab says:
As of MATLAB version 5.1, all development work for the DDE Server and Client has been stopped and no further development will be done. The MathWorks strongly recommends that you migrate to the MATLAB interface to COM technology that is documented.

In the past decade, DDE has been superseded by newer Windows technologies deriving from COM, which is supported by Matlab. However, DDE is still used for some Windows infrastructure functions such as cut/copy/paste (CCP) and the Windows Shell file association. Today’s article, based on information given to me by Martin Höcker, a reader of this blog, uses this fact to interface the Windows Explorer with Matlab.

Martin works on an experiment that outputs a large number of small data files that have a “THe” extension. He wrote a Matlab script that parses and plots the data files individually. This script is called “THePlotter.m” and is on the Matlab path. But calling this script with the command “THePlotter(‘D:\Data\somedata.THe’)” in Matlab’s console is tedious, especially if we need to check hundreds of files.

Since all data files have the same “.THe” extension, Martin associated this file type with Matlab so double-clicking any file in Explorer will plot the data in Matlab. It is very easy to set this up using DDE:

  1. associate the “.THe” file type with Matlab as shown below (note: use the MATLAB.exe file in the /bin/win32 or /bin/win64 subfolder). Now Matlab will generate an error for a double-click on the file, but we don’t care.
    Associating an extension with Matlab (click for details)

    Associating an extension with Matlab (click for details)

  2. in Windows Explorer, click on Tools / Folder Options / File Types
  3. select the “.THe” extension and click “Advanced”
    Editing the file association details

    Editing the file association details

  4. set this to be the default action upon mouse double-click or keyboard ENTER
  5. select the “open” action, and click “Edit…”
  6. rename the action “plot” (renaming is optional) and click the “Use DDE” checkbox
  7. enter the DDE information as seen below:

Windows Explorer settings (click for details)

Windows Explorer settings (click for details)

It is even possible to have several “actions” for the file: Martin has “Plot”, and “Add to plot”, which allows plotting multiple files at once by simply choosing “Add to plot” with a right click in Windows Explorer:

Windows Explorer use (click for details)

Windows Explorer use (click for details)

Behind the scenes, all this is simply a GUI wrapper for adding/modifying the Windows Registry:

Registry entry for the Shell file association

Registry entry for the Shell file association

Note that despite some information in the R13/R14 documentation, we do not need to run Matlab with the /Automation startup flag (or any other special modification for that matter), at least in recent Matlab releases.

The biggest advantage of using DDE: It does not open up a new instance of Matlab whenever you click on a file. This saves an incredible amount of start-up time.

One thing we cannot do, is to pass parameter/value pairs to the “THePlotter” function. The problem possibly lies in the way that Matlab parses the DDE commands – the parameter is enclosed by quotes, and Matlab seems to turn these quotes into double-quotes, and then chokes on itself…

Have you found another neat trick to enhance your work-flow? If so, please share it in the comments section below, or send me an email

p.s.: Readers who are interested in using Matlab’s DDE functionality programmatically, are welcome to read and use the following semi-documented built-in functions: ddeadv, ddeexec, ddeinit, ddepoke, ddereq, ddeterm, ddeunadv. These functions have a readable help section, but no doc page nor official support.

Categories: Medium risk of breaking in future versions, Semi-documented function, Undocumented feature

Tags: , , ,

Bookmark and SharePrint Print

7 Responses to Matlab DDE support

  1. Pooh says:

    Hi,

    Thanks a lot for this feature, It works well and will save me a lot of time (changing working directory, specify the file to open).

    I’ve a question about compiled matlab application : is it possible to call DDE with a matlab compiled app (MCR) ?

    I’ve a gui that read and process files and i deploy this gui in a compiled version. The non-compiled version can now open files directly from windows explorer (thx to this post). How can i do it with the compiled version if this is possible ?

    Thx a lot

    • @Pooh – I do not know how DDE behaves in a compiled application. I suspect that since MCR uses the same Matlab engine as the interactive application, DDE will also work with MCR, but I do not know. The easiest way to find out is to simply try it out (don’t worry – it will not explode in your face…)

    • Pooh says:

      Thx,

      Since last time i’ve made a script that manage all my DDE file association (initializatiojn by writing in the registry (even file icon) and open , plot of file)

      I’m certain i’ve read somewhere that compiled scripts and gui cannot have input argument.
      So i’m not quite sure how to use DDE.
      I’ll try and post after.

      I’ll also try mulitselection.

  2. Pooh says:

    Hi

    About compiled app:
    After compilation, there’s the *.exe
    -> myfun.exe
    I’ve associate my extension file with the executable (Step 1).
    When I open the file, myfun.exe is launched with one input arg: the path and file. It’s the same as %1 in dde.

    So, with a little adaptation, it’s easy to launch the file:

    function  myfun(args)
       if nargin==1 && exist(args{1},'file')==2
          launch_file(args{1})
          return;
       end
    end

    This code is ok for guis.

    I also wanted to explore multiselect open, but it seem a little bit more tricky: a multiselect on files then open just launch n-times the single file opening instruction.

  3. Jonas says:

    Cool stuff! About the “thing we cannot do”, i.e. passing several parameter/value pairs: From my tests with MATLAB R2010b it seems that MATLAB uses a greedy string parser here: everything between the first and last quote is treated as one string parameter. So, if your DDE command is THePlotter(‘filename.THe’, ‘somestring’, 2)
    then this is interpreted as two parameters:
    1) string: “filename.THe’, ‘somestring”
    and
    2) double: 2

    … so in principle not something you couldn’t recover from using eval().

  4. After upgrading to Matlab2013a, it took me a while to figure out that Mathworks has changed the DDE “Application Name”. It now includes the Matlab Version number, which for Matlab2013a makes it “ShellVerbs.Matlab.8.1.0.”. Just in case other people are wondering about this too, I made a small note on StackExchange.

  5. Pingback: Why does Windows explorer open multiple instances of Matlab2013a when I am using DDE-commands? | BlogoSfera

Leave a Reply


Your email address will not be published. Required fields are marked *