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
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
.
% Saved as get_plot_handle.m function h = get_plot_handle(lnSpec, lnWidth, mkEdge, mkFace, mkSize) h = @draw_plot; function draw_plot(x, y) plot(x, y, lnSpec, ... 'LineWidth', lnWidth, ... 'MarkerEdgeColor', mkEdge, ... 'MarkerFaceColor', mkFace, ... 'MarkerSize', mkSize) end end
% Saved as plot_xy.m function plot_xy(x, y, h) h(x, y); end
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); } } } }
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 two functions. The first creates a bank account for a customer based on some set of conditions. The second transfers funds between two accounts.
% Saved as account.m classdef account < handle properties name end properties (SetAccess = protected) balance = 0 number end methods function obj = account(name) obj.name = name; obj.number = round(rand * 1000); end function deposit(obj, deposit) new_bal = obj.balance + deposit; obj.balance = new_bal; end function withdraw(obj, withdrawl) new_bal = obj.balance - withdrawl; obj.balance = new_bal; end end end
% Saved as open_acct .m function acct = open_acct(name, open_bal ) acct = account(name); if open_bal > 0 acct.deposit(open_bal); end end
% Saved as transfer.m function transfer(source, dest, amount) if (source.balance > amount) dest.deposit(amount); source.withdraw(amount); end end
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.