| MATLAB® | ![]() |
| On this page… |
|---|
Executing Commands in the MATLAB Server Exchanging Data with the Server |
MATLAB functions and properties enable an Automation controller to manipulate data in the MATLAB workspace. MATLAB can be both a controller and a server. The examples in this section use a MATLAB M-file as the client application. For information about how to access a MATLAB server from other applications, see Examples of a MATLAB Automation Server.
This section explains how to call functions in the MATLAB Automation server and how to use properties that affect the server. These are shown in the following tables and are described in individual function reference pages.
For a complete list of these functions, see Component Object Model and ActiveX in the MATLAB Function Reference documentation.
The client program can execute commands in the MATLAB server using these functions.
| Function | Description |
|---|---|
Execute MATLAB command in server | |
Evaluate MATLAB command in server |
Use the Execute function when you want the MATLAB server to execute a command that can be expressed in a single string. For example:
h = actxserver('matlab.application');
h.PutWorkspaceData('A', 'base', rand(6))
h.Execute('A(4:6,:) = [];'); % remove rows 4-6
B = h.GetWorkspaceData('A', 'base')
MATLAB displays:
B =
0.6208 0.2344 0.6273 0.3716 0.7764 0.7036
0.7313 0.5488 0.6991 0.4253 0.4893 0.4850
0.1939 0.9316 0.3972 0.5947 0.1859 0.1146
Use the Feval function when you want the server to execute commands that you cannot express in a single string. The following example uses variables defined in the client, rows and cols, to modify the server.
This is a continuation of the previous example:
rows = 6; cols = 3;
h.Feval('reshape', 0, 'A=', rows, cols);MATLAB interprets A in the expression 'A=' as a server variable name.
The reshape function in the previous statement does not make an assignment to the server variable A; it is equivalent to the following MATLAB statement:
reshape(A,6,3)
which returns a result, but does not assign the new array. If you get the variable A from the server, it is unchanged:
B = h.GetWorkspaceData('A', 'base')
MATLAB displays:
B =
0.6208 0.2344 0.6273 0.3716 0.7764 0.7036
0.7313 0.5488 0.6991 0.4253 0.4893 0.4850
0.1939 0.9316 0.3972 0.5947 0.1859 0.1146Use the Feval function return value to get the result of this type of operation. For example, the following statement reshapes the server-side array A and returns the result of this operation in the client-side variable a:
a = h.Feval('reshape', 1, 'A=', rows, cols);
The Feval function returns a cell array. To view the contents, type:
a{:}
MATLAB displays:
ans =
0.6208 0.6273 0.7764
0.7313 0.6991 0.4893
0.1939 0.3972 0.1859
0.2344 0.3716 0.7036
0.5488 0.4253 0.4850
0.9316 0.5947 0.1146MATLAB provides functions to read and write data to any workspace of a MATLAB server. In these commands, pass the name of the variable to read or write, and the name of the workspace holding that data.
| Function | Description |
|---|---|
Get character array from server | |
Get matrix from server | |
Get any type of data from server | |
Store character array in server | |
Store matrix in server | |
Store any type of data in server |
The Get/PutCharArray functions read and write string values to the MATLAB server.
The Get/PutFullMatrix functions pass data as a SAFEARRAY data type. You can use these functions with any client that supports the SAFEARRAY type. This includes MATLAB and Visual Basic clients.
The Get/PutWorkspaceData functions pass data as a variant data type. Use these functions with any client that supports the variant type. These functions are especially useful for VBScript clients because VBScript does not support the SAFEARRAY data type.
In this example, write a string to variable str in the base workspace of the MATLAB server and read it back to the client:
h = actxserver('matlab.application');
h.PutCharArray('str', 'base', ...
'He jests at scars that never felt a wound.');
S = h.GetCharArray('str', 'base')
S =
He jests at scars that never felt a wound.These functions enable you to make the server window visible or to minimize it.
| Function | Description |
|---|---|
Display server window on Windows desktop | |
Minimize size of server window |
In this example, create a COM server running MATLAB and minimize it:
h = actxserver('matlab.application');
h.MinimizeCommandWindow;When you are finished with the MATLAB server, use these functions to quit the MATLAB session and terminate the server process.
| Function | Description |
|---|---|
Quit the MATLAB session | |
Terminate MATLAB server process |
To quit MATLAB, type:
h.Quit;
To terminate the server process, type:
h.delete;
This section provides information specific to MATLAB and Visual Basic .NET clients only.
To see a summary of all functions along with the required syntax, use the invoke function as follows:
handle = actxserver('matlab.application');
handle.invokeData types for the arguments and return values of the server functions are expressed as Automation data types, which are language-independent types defined by the Automation protocol.
For example, BSTR is a wide-character string type defined as an Automation type, and is the same data format used by the Visual Basic language to store strings. Any COM-compliant controller should support these data types, although the details of how you declare and manipulate these are controller specific.
You have the option of making MATLAB visible or not by setting the Visible property. When visible, MATLAB appears on the desktop, enabling the user to interact with it. This might be useful for such purposes as debugging. When not visible, the MATLAB window does not appear, thus perhaps making for a cleaner interface and also preventing any interaction with the application.
By default, the Visible property is enabled, or set to 1:
h = actxserver('matlab.application');
h.Visible
ans =
1You can change the Visible property by setting it to 0 (invisible) or 1 (visible). The following command removes the server application window from the desktop:
h.Visible = 0;
h.Visible
ans =
0
![]() | Introduction | Additional Automation Server Information | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |