When developing applications that use multiple MATLAB® shared libraries, consider the following:
Each MATLAB shared library must be initialized separately.
Each MATLAB shared library must be terminated separately.
MATLAB function handles cannot be shared between shared libraries.
MATLAB figure handles cannot be shared between shared libraries.
MATLAB objects cannot be shared between shared libraries.
C, Java®, and .NET objects cannot be shared between shared libraries.
Executable data stored in cell arrays and structures cannot be shared between shared libraries
To initialize and terminate multiple shared libraries:
Initialize the MATLAB Runtime using
Call the portion of the application that executes the MATLAB code using
Before initializing the shared libraries, initialize the MATLAB application state using
For each MATLAB shared library, call the generated initialization function,
Add the code for working with the MATLAB code.
For each MATLAB shared library, release the resources used by the library using
the generated termination function,
Release the resources used by the MATLAB Runtime by calling
This example shows the use of two shared libraries.
A MATLAB function handle can be passed back and forth between a MATLAB Runtime instance and an application. However, it cannot be passed from one
MATLAB Runtime instance to another. For example, suppose that you had two MATLAB functions,
plot_xy used the function 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 packaged them into two separate shared libraries, the call to
plot_xy would throw an exception.
One way to handle the situation is to package both functions into a single shared
library. For example, if you called the shared library
plot_functions, your application would only need one call to
initialize the function and you could pass the function handle for
plot_xy without error.
MATLAB Compiler SDK™ enables you to return the following types of objects from the MATLAB Runtime to your application code:
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 packaged into different shared libraries.
For example, say that you develop two functions. The first creates a bank account for a customer. 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 packaged
separate shared libraries, you could not transfer funds using accounts created with
open_acct. The call to
transfer would throw an
exception. One way of resolving this is to package both functions into a single shared
library. You could also refactor the application so as not to pass MATLAB objects to the functions.