Programming in MATLAB
MATLAB Programmer
| Role | Knowledge
Base | Responsibilities |
MATLAB
programmer | MATLAB expert No IT experience No access to IT systems
| Develops models; implements in MATLAB Serves as tool builder Uses tools to create a component
that is used by the Java developer Uses tools to create a component that
is used by the .NET developer Uses tools to create a component
that is used by the COM developer Uses tools to create a component
that is used by the C or C++ developer
|
MATLAB is an interpreted programming environment. You
can execute functions directly at the command prompt or through an
editor in saved files. Methods may be created, having their own unique
inputs and outputs. When deploying a MATLAB function to other
programming environments, such as .NET and Java, you must contain
your MATLAB code within functions. MATLAB Compiler does not allow
you to use inline scripts.
The following examples demonstrate how to perform basic MATLAB programmer
tasks for deployed applications; they do not attempt to represent
every way a MATLAB programmer can interface with MATLAB.
Upcoming topics demonstrate how to use various data types in deployed
applications. For more specific information about any of these data
types, see the documentation for the product you are using (MATLAB Compiler, MATLAB Builder
JA, or MATLAB Builder
NE)
MATLAB Programming Basics
Creating a Deployable MATLAB Function
Virtually any calculation that you can create in MATLAB can
be deployed, if it resides in a function. For example:
>> 1 + 1
cannot be deployed.
However, the following calculation:
function result = addSomeNumbers()
result = 1+1;
end
can be deployed because the calculation now resides in a function.
Taking Inputs into a Function
You typically pass inputs to a function. You can use primitive
data type as an input into a function.
To pass inputs, put them in parentheses. For example:
function result = addSomeNumbers(number1, number2)
result = number1 + number2;
end
Back to Top
Returning MATLAB Data Types
MATLAB allows many different deployable data types. This
section contains examples of how to work with figures. For an in-depth
explanation of how to work with MATLAB primitive data types,
see the MATLAB External Interfaces documentation.
MATLAB Figures
Often, you are dealing with images displayed in a figure window,
and not just string and numerical data. Deployed Web applications
can support figure window data in a number of ways. By using the WebFigures infrastructure (see Deploying a Java Component Over the Web in the MATLAB Builder JA User's
Guide or Web Deployment of Figures and Images in
the MATLAB Builder NE User's Guide), the respective builder marshals
the data for you.
Alternatively, you can take a snapshot of what is in the figure
window at a given point and convert that data into the raw image data
for a specific image type. This is particularly useful for streaming
the images across the web.
Returning Data from a WebFigure Window
WebFigures is
a feature that enables you to embed dynamic MATLAB figures onto
a Web page through a Builder JA or Builder NE component. This concept
can be used with any data in a figure window.
As in the following example, you close the figure before the
code is exited so that the figure does not "pop up,"
or appear later, in the deployed application. You do not need to
specify any reorientation data when using WebFigures. If the figure
is attached to the rest of the infrastructure, it will automatically
pass, resize, and reorient accordingly.
%returns a WebFigure reference containing the
%data from the figure window
function resultWebFigure = getWebFigure
f = figure;
set(f,'Color',[.8,.9,1]);
f = figure('Visible','off');
surf(peaks);
resultWebFigure = webfigure(f);
close(f);
end
Returning a Figure as Data
This approach is typically used for instances where WebFigures can't be used, or in a stateless application.
%We set the figure not to be visible since we are
%streaming the data out
%Notice how you can specify the format of the bytes,
% .net uses unsigned bytes (uint8)
% java uses signed bytes (int 8)
%This function allows you to specify the image format
%such as png, or jpg
function imageByteData = getSurfPeaksImageData(imageFormat)
f = figure;
surf(peaks);
set(f, 'Visible', 'off');
imageByteData = figToImStream(f, imageFormat, 'uint8');
close(f);
end
Reorienting a Figure and Returning It as Data
Sometimes you want the function to change the perspective on
an image before returning it. This can be accomplished like this:
%We set the figure not to be visible since we are
%streaming the data out
%Notice how you can specify the format of the bytes,
% .net uses unsigned bytes (uint8)
% java uses signed bytes (int 8)
%This function allows you to specify the image format
%such as png, or jpg
function imageData =
getImageDataOrientation(width, height, rotation,
elevation, imageFormat)
f = figure('Position', [0, 0, width, height]);
surf(peaks);
view([rotation, elevation]);
set(f, 'Visible', 'off');
imageData = figToImStream (f, imageFormat, 'uint8');
close(f);
end
Back to Top
 | MATLAB Programmer Tasks | | Deploying MATLAB Code with the Builders |  |
Learn how to build standalone executables and C/C++ shared libraries from MATLAB code.
Get free kit