| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB Builder NE |
| Contents | Index |
| Learn more about MATLAB Builder NE |
| On this page… |
|---|
The purpose of the example is to show you the following:
How to use the MATLAB Builder NE product to create a component (SpectraComp) containing more than one class
How to access the component in a C# application (SpectraApp.cs), including use of the MWArray class hierarchy to represent data
Note For complete reference information about the MWArray class hierarchy, see the MWArray Class Library Reference (available online only). |
How to build and run the application, using the Visual Studio .NET development environment
The component SpectraComp analyzes a signal and graphs the result. The class, SignalAnalyzer, 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, Plotter, graphs the returned data using the plotfft method. These two methods, computefft and plotfft, encapsulate MATLAB functions.
The 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 interval. The 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 M-files, computefft.m and plotfft.m, which can be found in:
matlabroot\toolbox\dotnetbuilder\Examples\VS8\NET\SpectraExample\SpectraComp
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:
matlabroot\toolbox\dotnetbuilder\Examples\VS8\NET\SpectraExample
At the MATLAB command prompt, cd to the new SpectraExample subfolder in your work folder.
Write the M-code that you want to access.
This example uses computefft.m and plotfft.m, which are already in your work folder in SpectraExample\SpectraComp.
While in MATLAB, issue the following command to open the Deployment Tool window:
deploytool
Build the .NET component. See the instructions in Building Your Component for more details. Use the following information:
| Project Name | SpectraComp |
| Class Names | PlotterSignalAnalyzer |
| Files to compile | computefft.mplotfft.m |
Write source code for an application that accesses the component.
The sample application for this example is in SpectraExample\SpectraCSApp\SpectraApp.cs.
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
Creates an MWNumericArray array that contains the data
Instantiates a SignalAnalyzer object
Calls the computefft method, which computes the FFT, frequency, and the spectral density
Instantiates a Plotter object
Calls the plotfft method, which plots the data
Uses a try/catch block 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 computefft function.
The following statement
SignalAnalyzer signalAnalyzer = new SignalAnalyzer();
creates an instance of the class SignalAnalyzer, and the following statement
MWArray[] argsOut= signalAnalyzer.computefft(3, data, interval);
calls the method computefft.
Build the SpectraApp application using Visual Studio .NET.
The SpectraCSApp folder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clicking SpectraCSApp.csproj in Windows Explorer. You can also open it from the MATLAB desktop by right-clicking SpectraCSApp.csproj > Open Outside MATLAB.
Add a reference to the MWArray component,
which is matlabroot\toolbox\dotnetbuilder\bin\architecture\framework_version
\mwarray.dll.
If necessary, add (or fix the location of) a reference to the SpectraComp component which you built in a previous step. (The component, SpectraComp.dll, is in the \SpectraExample\SpectraComp\x86\V2.0\Debug\distrib subfolder of your work area.)
Build and run the application in Visual Studio .NET.
![]() | Passing Variable Arguments | Matrix Math Example | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |