A .NET-based user interface for MATLAB applications

logo_matlab MATLAB is a numerical computing environment for engineering applications. It supports communicating with and controlling hardware and processing and visualizing data from that hardware, which makes it a very popular platform for writing the software that goes with custom hardware solutions.  It also supports creating user interfaces too, but they tend to look outdated and clunky. matlabGUI_example1a

For example, the control and display software for your custom pump controller system can easily be written in MATLAB. It would run on a PC that is connected to your hardware, lets the user turn the system on and off, set parameters such as pump cycle delays and display graphs showing overall efficiency or total energy input. You can compile all the MATLAB code and sell it, along with your hardware and the MATLAB compiler runtime, as a product.

But what are your options when you want a sleeker, more modern UI with a ribbon bar, interactive graphs and UI themes or skins?  The most economic solution with a relatively short time-to-market, is to create a .NET based UI which connects to your MATLAB functions. In this article, I’ll elaborate on how exactly this works.

The MATLAB programming language is also called ‘m-code’  and is saved in files with an .m extension. You can compile these m-files into a .NET dll using MATLAB builder NE.  In the examples below,  MATLAB has compiled all m-files into MatlabExample.dll (providing methods in the MatlabExample namespace), and the .NET C# project has a reference to this dll.

First, lets look at an m-file, loading an MyObject file from the local filesystem (or creating a default MyObject if the file does not exist yet):

function [MyObject,Error]=LoadFromFile(Id) % Create a Structure with the Default Settings Error=0; loadpath=strcat(Setting.Path,Id,Setting.MyObjectExtension); % Check if File exists if exist(loadpath,'file')==2 %File exists load(loadpath,'-mat'); else MyObject=CreateDefaultObject(Id); Error=202; end end

The MyObject file is a file saved by MATLAB, using a binary format as in the example below:

function Create(MyObject)

% Creates and saves a MyObject

MyObject.Id = '123';

MyObject.Foo = 1.2;

MyObject.Bar = 9.99;

MyObject.LoremIpssum = 3.14;

MyObject.Name = 'sample';

savePath=strcat(Setting.Path,MyObject.Id, Setting.MyObjectExtension);



The following C# uses the m-code and wraps its functionality into a MyObject class :

Page 1 of 4 | Next page