6 Matlab tips for .NET users

TipsMatlab is a great big machine, but sometimes hard to work with. Even without any toolbox, there are so many features that it is hard to find information although the help section is really useful ! Here are some tips I regularly use in my daily work.

Tip 1 - Maximize a graph window

This tip is used when you have to maximize the output figure window. You can always try to get the screen size with the command  screensize = get( 0, 'Screensize' ); and apply it to your current figure, but the window is not maximized, it just fits the whole screen. To do so, not Matlab embedded function is provided so you have to call the Java equivalent :

Tip 2 - Create a data table from variables

Sometimes, you need to represent your data as a table. Tables (or formely datasets in the statistics Toolbox) are useful and practical to manipulate, and tend to be similar to datasets we find in R. As a simple and straighforward example, here is a way to declare and construct a table from variables

where  var1 ,  var2  and   var3  are variables we want to include in the table.

Example taken from the Matlab help:


Tip 3 - Export data table to Excel

Once a table has been created, a very useful trick is to export it to Excel for further analysis or to share with someone who has not Matlab or is not familiar with. Here again, it is quite straightforward


Tip 4 - Work with several axes without subplot

Sometimes, I need to plot two graphs on the same figure. For that, I generally use the  subplot  function which suits very well. However, I wanted to plot dual/triple axes in one subplot few days ago so I needed another solution. Function  plotyy  could help with two axes plots, but what about the three axes case ? Here the solution, use the  axes  function and customize our axes


Tip 5 - Use your own .NET assemblies !

One thing I find particularly cool with Matlab, is that it is very easy to work with .NET assemblies so that you can use your own assemblies (dll). I personnaly write all my code in C# but I frequently need to prototype mathematical analysis on my data or visualize/plot graph to explain some phenomenon. Matlab is of great help here again as I can load my own assemblies (class containers, file loading, analysis class prototype etc.) and play with it in a intuitive scripting language (just as Python does.....). The way to declare an assembly at runtime is easy :


Tip 6 - Register your assemblies

A problem I had when I began using Matlab to work with my osn assemblies, is that their are not in the GAC neither in the Matlab directory so that they are not well resolved. For example, I often faced with a serialization exception that my type was not known. This happens when an assembly depends on another assembly, even if both are declared at runtime. Here are some great and helpful articles on the subject and various proposed solutions.

Create instance of Type Outside

Microsoft Help Center

MSDN Article : Specifying an Assembly's Location

MSDN Article : How the Runtime Locates Assemblies

As I did not want to include my assemblies in the GAC and neither in the Matlab root directory, I added an event listener on the  AssemblyResolve . Embedded in a static method, this is the first thing I call when working with my own assemblies. The beauty is that I only have to call it once and the magic happens


Leave a Reply

%d bloggers like this: