Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Creating Custom File Associations

Asked by Greg on 22 Jul 2011

I work with data files that have their only file extension. We have created a large set of tools that decodes the data and allows us to perform our analysis. What I am trying to do in is associate this file type with MATLAB, so if I click on one of these files (I am using Windows 7) it will automatically and run the tool on the file.

I have written an open function for my file type which works fine when called from inside MATLAB and I can tell Windows to associate the file type with MATLAB, but when I click on the file it just opens MATLAB (regardless of whether it is already opened) and does nothing. Is there a way to fix this so it knows to run my openxzy function when it is trying to open a .xyz file?

Any help or suggestions are appreciated.

Greg

1 Comment

Greg on 4 Aug 2011

By diggering around in the registary I was able to find how to create a custom open with command. So when one of my files is clicked it calls matlab.exe -r open('"%1"'). However this always opens a new MATLAB session. I would like it to open in the existing session if there is one. Looking at how .m and .mat are opened in the registary, MATLAB is called from a different location (it has an @ on the font) and it passes a numberic option ( -58 for .m and -59 for .mat). Does anyone know anything about this or what it means?

Greg

Products

No products are associated with this question.

1 Answer

Answer by Greg on 18 Aug 2011
Edited by Greg on 19 Mar 2013
Accepted answer

I figured it out. First, and really the only MATLAB part, create an openxzy.m file and place it in MATLAB's startup path (probably Documents/MATLAB). Then in windows registry you have to replicate what .m, .mat, etcs files do. Here is what I have in my .reg file with explanation below:

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.xyz]
@="xyzfile"
[HKEY_CLASSES_ROOT\.xyz\OpenWithList]
@=""
[HKEY_CLASSES_ROOT\.xyz\OpenWithList\matlab.exe]
@=""
[HKEY_CLASSES_ROOT\.xyz\OpenWithProgids]
@=""
[HKEY_CLASSES_ROOT\.xyz\OpenWithProgids\xyzfile]
@=""
[HKEY_CLASSES_ROOT\xyzfile]
@="XYZ Data"
[HKEY_CLASSES_ROOT\xyzfile\Shell]
[HKEY_CLASSES_ROOT\xyzfile\Shell\Open]
@="Open"
[HKEY_CLASSES_ROOT\xyzfile\Shell\Open\command]
@="\"C:\\Program Files\\MATLAB\\\\R2010a\\bin\\win64\\matlab.exe\""
[HKEY_CLASSES_ROOT\xyzfile\Shell\Open\ddeexec]
@="open('%1')"
[HKEY_CLASSES_ROOT\xyzfile\Shell\Open\ddeexec\application]
@="shellverbs.MATLAB"
[HKEY_CLASSES_ROOT\xyzfile\Shell\Open\ddeexec\topic]
@="system"

This is more complicated than what is necessary to get it to work. You need to add two keys to the HKEY_CLASSES_ROOT (which requires admin privilages). One is for the exentension (.xyz). The default vaule of .xyz (xyzfile) is where windows will look when trying to open that file. The rest in that key is just making the open with menu look how I want it to.

xyzfile needs a subkey Shell, Shell needs a subkey Open (you can actually have others, just make the default value of Shell the same as the name of the subkey you want as a default command), Open needs a subkey command. The default value of command needs to be the full path of matlab.exe (which can also be followed with commands like -r). That is enough to get it working. The magic happens when you add the the subkey ddeexec with its two subkeys (application and topic with default values shellverbs.MATLAB and system respectively) to the Open key. You set the default value of ddeexec to the function you want called (%1 is Windows' veraible name for the file name) and it will be called in the existing MATLAB session if there is one and open one otherwise.

Since figuring this out I have easily added icons to .xyz files and functions to the right click menu of .mat files.

2 Comments

Carl on 8 Feb 2013

This is great! I got my custom extension to work fine is Matlab is already up and running.

However if it is not running I immediatly get an error that the file I just clicked was not found, Matlab opens and nothing happens, not even an error message. I think this might be due to the file using programs that are run during the initialisation phase of matlab. Is there a way to postpone the opening of the file until after the init phase is completed?

Greg on 19 Mar 2013

I am not sure. However I am now trying to do the same thing in Linux. I am trying to figure out how to get it to open the file without opening a new session of MATLAB, so I will be trying to learn more about what shellverbs.MATLAB does for Windows.

Greg

Contact us