The purpose of the example is to show you the following:
How to access the component in a C# application (
including use of the
MWArray class hierarchy to
For information about these data conversion classes, see the MATLAB
MWArray Class Library Reference, available in the
matlabroot represents your MATLAB installation
How to build and run the application, using the Visual Studio® .NET development environment
SpectraComp analyzes a signal
and graphs the result. The class,
performs a fast Fourier transform (FFT) on an input data array. A
method of this class,
computefft, returns the results
of that FFT as two output arrays—an array of frequency points
and the power spectral density. The second class,
graphs the returned data using the
These two methods,
encapsulate MATLAB functions.
computefft method computes the FFT and
power spectral density of the input data and computes a vector of
frequency points based on the length of the data entered and the sampling
plotfft method plots the FFT data
and the power spectral density in a MATLAB figure window. The MATLAB code
for these two methods resides in two MATLAB files,
which can be found in:
If you have not already done so, copy the files for this example as follows:
Copy the following folder that ships with the MATLAB product to your work folder:
At the MATLAB command prompt,
SpectraExample subfolder in your work folder.
Write the MATLAB code that you want to access.
This example uses
which are already in your work folder in
From the MATLAB apps gallery, open the Library Compiler app.
Build the .NET component. See the instructions in Generate a .NET Assembly and Build a .NET Application for more details. Use the following information:
|Files to compile|
Write source code for an application that accesses the component.
The sample application for this example is in
The program listing is shown here.
The program does the following:
Constructs an input array with values representing a random signal with two sinusoids at 15 and 40 Hz embedded inside of it
that contains the data
computefft method, which
computes the FFT, frequency, and the spectral density
plotfft method, which
plots the data
to handle exceptions
The following statement
MWNumericArray data= new MWNumericArray(MWArrayComplexity.Real, MWNumericType.Double, numSamples);
shows how to use the
MWArray class library
to construct a
MWNumericArray that is used as method
input to the
The following statement
SignalAnalyzer signalAnalyzer = new SignalAnalyzer();
creates an instance of the class
and the following statement
MWArray argsOut= signalAnalyzer.computefft(3, data, interval);
calls the method
SpectraApp application using Visual
SpectraCSApp folder contains a Visual
project file for this example. Open the project in Visual
SpectraCSApp.csproj in Windows® Explorer.
You can also open it from the desktop by right-clicking SpectraCSApp.csproj > Open Outside
Add a reference to the
See Supported Microsoft .NET Framework Versions for
a list of supported framework versions.
If necessary, add (or fix the location of) a reference
SpectraComp component which you built in
a previous step. (The component,
is in the
of your work area.)
Build and run the application in Visual Studio .NET.