In: Electrical Engineering
q1) using Matlab make a code that allows a user to enter a function like fractional function and cubic liner function then the Matlab send a domain, range, continuity
Before you use any MATLAB Compiler element, initialize the
supporting libraries with the current example of Microsoft®
Excel®. Do this once for an Excel session that makes use of the
MATLAB Compiler components.
To do this initialization, name the utility library operate
MWInitApplication, which is a member of the MWUtil class. This
class is part of the MWComUtil library. See classification MWUtil
.
A method so as to add this initialization code into a VBA module is
to provide a subroutine that does the initialization once, and
effectively exits for all subsequent calls. The next Microsoft
visual basic® code sample initializes the libraries with the
present instance of Excel. A global variable of form Object named
MCLUtil holds an instance of the MWUtil type, and one other world
variable of style Boolean named bModuleInitialized retailers the
reputation of the initialization approach. The personal subroutine
InitModule() creates an instance of the MWComUtil type and calls
the MWInitApplication method with an argument of software. Once
this function succeeds, all subsequent calls exit with out
reinitializing.
Dim MCLUtil As Object
Dim bModuleInitialized As Boolean
private Sub InitModule()
If no longer bModuleInitialized Then
On Error GoTo Handle_Error
If MCLUtil Is Nothing Then
Set MCLUtil = CreateObject("MWComUtil.MWUtil")
finish If
call MCLUtil.MWInitApplication(utility)
bModuleInitialized = actual
Exit Sub
Handle_Error:
bModuleInitialized = False
end If
end Sub
This code is just like the default initialization code generated
within the VBA module created when the component is constructed.
Each and every operate that uses MATLAB Compiler add-ons can
include a call to InitModule at
the establishing to be certain that the initialization constantly
will get carried out as needed.
Create an instance of a class
Overview
CreateObject operate
New Operator
How the MATLAB Runtime Is Shared amongst classes
Overview
earlier than calling a category system (compiled MATLAB operate),
you need to create an illustration of the category that comprises
the approach. VBA presents two strategies for doing this:
CreateObject operate
New operator
CreateObject perform
This method makes use of the Microsoft visual normal utility
programming interface (API) CreateObject perform to create an
example of the class. Microsoft refers to calling CreateObject as
late binding and using new as early binding.
To use this process, declare a variable of form Object utilizing
Dim to hold a reference to the class example and make contact with
CreateObject utilizing the class programmatic identifier (ProgID)
as an argument, as shown within the subsequent illustration:
perform foo(x1 As Variant, x2 As Variant) As Variant
Dim aClass As Object
On Error Goto Handle_Error
Set aClass = CreateObject("mycomponent.Myclass.1_0")
' (name some approaches on aClass)
Exit perform
Handle_Error:
foo = Err.Description
end function
New Operator
This procedure uses the visible general New operator on a variable
explicitly dimensioned as the class to be created. Earlier than
utilising this method, you have got to reference the kind library
containing the category in the current VBA undertaking. Do that
through settling on the instruments menu from the visible general
Editor, after which identifying References to show the on hand
References record. From this list, decide on the necessary form
library.
The next instance illustrates using the brand new operator to
create a class example. It assumes that you've got selected
mycomponent 1.0 form Library from the on hand References list
earlier than calling this function.
Operate foo(x1 As Variant, x2 As Variant) As Variant
Dim aClass As mycomponent.Myclass
On Error Goto Handle_Error
Set aClass = New mycomponent.Myclass
' (call some methods on aClass)
Exit function
Handle_Error:
foo = Err.Description
finish perform
on this illustration, the class instance will also be dimensioned
as comfortably myclass. The whole statement within the type
<component-name>.<class-name> guards against title
collisions
in the previous two examples, the category instance used to make
the system name was a neighborhood variable of the method. This
creates and destroys a brand new category instance for each call.
An alternative method is to declare one single module-scoped
category instance that's reused by all perform calls, as in the
initialization code of the previous example.
The following example illustrates this procedure with the 2d
system:
Dim aClass As mycomponent.Myclass
perform foo(x1 As Variant, x2 As Variant) As Variant
On Error Goto Handle_Error
If aClass Is Nothing Then
Set aClass = New mycomponent.Myclass
end If
' (name some approaches on aClass)
Exit operate
Handle_Error:
foo = Err.Description
end perform
How the MATLAB Runtime Is Shared amongst lessons
MATLAB Compiler creates a single MATLAB Runtime when
the first Microsoft COM type is instantiated in an software. This
MATLAB Runtime is reused and shared among all subsequent
classification occasions within the component, resulting in more
efficient reminiscence usage and eliminating the MATLAB Runtime
startup rate in each and every subsequent class
instantiation.
All type instances share a single MATLAB workspace and share global
variables within the MATLAB documents used to construct the aspect.
This makes houses of a COM class behave as static properties as an
alternative of illustration-intelligent properties.
Call the approaches of a class example
after getting created a class illustration, that you could call the
class methods to access the compiled MATLAB functions. MATLAB
Compiler applies a general mapping from the fashioned MATLAB
function syntax to the system's argument record. See Reference
Utility courses for a targeted description of
the mapping from MATLAB features to COM category system
calls.
When a process has output arguments, the primary argument is
perpetually nargout, which is of form long. This enter parameter
passes the usual MATLAB nargout parameter to the
compiled operate and specifies what number of outputs are
requested. Ways that do not have output arguments do not go a
nargout argument. Following nargout are the output parameters
listed in the equal order as they appear on the left aspect of the
fashioned MATLAB operate. Next come the input parameters listed
within the identical order as they appear on the right side of the
original MATLAB operate. All enter and output arguments are typed
as Variant, the default visible common information sort.
The Variant sort can preserve any of the fundamental VBA forms,
arrays of any kind, and object references. See information
Conversion principles for a unique description of how to transform
Variant types of any common form to and from MATLAB knowledge
types. Ordinarily, you can provide any visible basic form as an
argument to a class procedure, aside from visual normal UDTs. You
could additionally go Microsoft Excel variety objects straight as
input and output arguments.
Whilst you cross a simple Variant type as an output parameter, the
known as approach allocates the received data and frees the
original contents of the Variant. On this case it's adequate to
dimension each and every output argument as a single Variant. When
an object type (like an Excel variety) is handed as an output
parameter, the item reference is handed in each directions, and the
item's price property receives the information.
The following examples illustrate the method of passing enter and
output parameters from VBA to the MATLAB Compiler element class
methods.
The primary illustration is a formulation perform that takes two
inputs and returns one output. This function dispatches the call to
a class procedure that corresponds to a MATLAB operate of the form
operate y = foo(x1,x2).
Operate foo(x1 As Variant, x2 As Variant) As Variant
Dim aClass As Object
Dim y As Variant
On Error Goto Handle_Error
Set aClass = New mycomponent.Myclass
aClass = CreateObject("mycomponent.Myclass.1_0")
name aClass.Foo(1,y,x1,x2)
foo = y
Exit perform
Handle_Error:
foo = Err.Description
finish function
The second illustration rewrites the identical operate as a
subroutine and uses Excel levels for enter and output.
Sub foo(Rout As variety, Rin1 As variety, Rin2 As variety)
Dim aClass As Object
On Error Goto Handle_Error
aClass = CreateObject("mycomponent.Myclass.1_0")
name aClass.Foo(1,Rout,Rin1,Rin2)
Exit Sub
Handle_Error:
MsgBox(Err.Description)
finish Sub
application with Variable Arguments
procedure varargin and varargout Arguments
When varargin and/or varargout are gift within the MATLAB function
that you're using for the Excel element, these parameters are
delivered to the argument list of the class method because the last
input/output parameters within the record. You can cross more than
one arguments as a varargin array via creating a Variant array,
assigning every element of the array to the respective input
argument.
The next instance creates a varargin array to call a system due to
a MATLAB function of the form y = foo(varargin):
function foo(x1 As Variant, x2 As Variant, x3 As Variant, _
x4 As Variant, x5 As Variant) As Variant
Dim aClass As Object
Dim v As Variant
Dim y As Variant
Dim MCLUtil As Object
On Error GoTo Handle_Error
set aClass = CreateObject("mycomponent.Myclass.1_0")
Set MCLUtil = CreateObject("MWComUtil.MWUtil")
call MCLUtil.MWPack(v, x1, x2, x3, x4, x5)
name aClass.Foo(1, y, v)
foo = y
Exit perform
Handle_Error:
foo = Err.Description
finish operate
The MWUtil class integrated in the MWComUtil utility library
presents the MWPack helper perform to create varargin parameters.
See classification MWUtil for more small print.
The subsequent instance methods a varargout parameter into three
separate Excel tiers. This operate makes use of the MWUnpack
function within the utility library. The MATLAB function used is
varargout = foo(x1,x2).
Sub foo(Rout1 As variety, Rout2 As range, Rout3 As range, _
Rin1 As variety, Rin2 As variety)
Dim aClass As Object
Dim aUtil As Object
Dim v As Variant
On Error Goto Handle_Error
aUtil = CreateObject("MWComUtil.MWUtil")
aClass = CreateObject("mycomponent.Myclass.1_0")
call aClass.Foo(three,v,Rin1,Rin2)
call aUtil.MWUnpack(v,zero,real,Rout1,Rout2,Rout3)
Exit Sub
Handle_Error:
MsgBox(Err.Description)
end Sub