Main Content

Limitations on Multiple Assemblies in Single Application

When developing applications that use multiple MATLAB® .NET assemblies, consider that the following cannot be shared between assemblies:

  • MATLAB function handles

  • MATLAB figure handles

  • MATLAB objects

  • C, Java®, and .NET objects

  • Executable data stored in cell arrays and structures

Work with MATLAB Function Handles

MATLAB function handles can be passed between an application and the MATLAB Runtime instance from which it originated. However, a MATLAB function handle cannot be passed into a MATLAB Runtime instance other than the one in which it originated. For example, suppose you had two MATLAB functions, get_plot_handle and plot_xy, and plot_xy used the function handle created by get_plot_handle.

 get_plot_handle.m

 plot_xy.m

If you compiled them into two shared libraries, the call to plot_xy would throw an exception.

using System;

using MathWorks.MATLAB.NET.Utility;
using MathWorks.MATLAB.NET.Arrays;

using get_plot_handle;
using plot_xy;

namespace MathWorks.Examples.PlotApp
 {
  class PlotCSApp
    {
      static void Main(string[] args)
        {
          try
            {
              // Create objects for the generated functions
              get_plot_handle.Class1 plotter= 
                new get_plot_handle.Class1();
              plot_xy.Class1 plot = new plot_xy.Class1();

              MWArray h = plotter.get_plot_handle('--rs', (double)2,
                'k','g', (double)10);

              double[] x_data = {1,2,3,4,5,6,7,8,9};
              double[] y_data = {2,6,12,20,30,42,56,72,90};
              MWArray x = new MWArray(x_data);
              MWArray y = new MWArray(y_data);
              plot.plot_xy(x, y, h);
            }

          catch(Exception exception)
            {
              Console.WriteLine("Error: {0}", exception);
            }
        }
    }
  }

The correct way to handle the situation is to compile both functions into a single assembly.

using System;

using MathWorks.MATLAB.NET.Utility;
using MathWorks.MATLAB.NET.Arrays;

using plot_functions;

namespace MathWorks.Examples.PlotApp
 {
  class PlotCSApp
    {
      static void Main(string[] args)
        {
          try
            {
              // Create object for the generated functions
              Class1 plot= new Class1();

              MWArray h = plot.get_plot_handle('--rs', (double)2,
                'k','g', (double)10);

              double[] x_data = {1,2,3,4,5,6,7,8,9};
              double[] y_data = {2,6,12,20,30,42,56,72,90};
              MWArray x = new MWArray(x_data);
              MWArray y = new MWArray(y_data);
              plot.plot_xy(x, y, h);
            }

          catch(Exception exception)
            {
              Console.WriteLine("Error: {0}", exception);
            }
        }
    }
  }
 

Work with Objects

MATLAB Compiler SDK™ enables you to return the following types of objects from the MATLAB Runtime to your application code:

  • MATLAB

  • C++

  • .NET

  • Java

However, you cannot pass an object created in one MATLAB Runtime instance into a different MATLAB Runtime instance. This conflict can happen when a function that returns an object and a function that manipulates that object are compiled into different assemblies.

For example, you develop a bank account class and two functions. The first creates a bank account for a customer based on some set of conditions. The second transfers funds between two accounts.

 account.m

 open_acct .m

 transfer.m

If you compiled open_acct.m and transfer.m into separate assemblies, you could not transfer funds using accounts created with open_acct. The call to transfer throws an exception.

One way of resolving this is to compile both functions into a single assembly. You could also refactor the application such that you are not passing MATLAB objects to the functions.