Main Content

Multiple Classes in a .NET Component

Purpose

The purpose of the example is to show you the following:

  • How to use the MATLAB® Compiler SDK™ product to create an assembly (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 information about these data conversion classes, see the MATLAB MWArray Class Library Reference, available in the matlabroot\help\dotnetbuilder\MWArrayAPI folder, where matlabroot represents your MATLAB installation folder

  • 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 MATLAB files, computefft.m and plotfft.m, which can be found in:

matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\SpectraExample\SpectraComp

 computefft.m

 plotfft.m

Procedure

  1. If you have not already done so, copy the files for this example as follows:

    1. Copy the following folder that ships with the MATLAB product to your work folder:

      matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\SpectraExample
      
    2. At the MATLAB command prompt, cd to the new SpectraExample subfolder in your work folder.

  2. Write the MATLAB code that you want to access.

    This example uses computefft.m and plotfft.m, which are already in your work folder in SpectraExample\SpectraComp.

  3. From the MATLAB apps gallery, open the Library Compiler app.

  4. Build the .NET component. See the instructions in Generate a .NET Assembly and Build a .NET Application for more details. Use the following information:

    Project NameSpectraComp
    Class NamesPlotter    SignalAnalyzer
    Files to compilecomputefft.m    plotfft.m

  5. 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.

     SpectraApp.cs

    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.

  6. Build the SpectraApp application using Visual Studio .NET.

    1. 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 desktop by right-clicking SpectraCSApp.csproj > Open Outside MATLAB.

    2. Add a reference to the MWArray component, which is matlabroot\toolbox\dotnetbuilder\bin\architecture\framework_version
      \mwarray.dll.
      See Supported Microsoft .NET Framework Versions for a list of supported framework versions.

    3. 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.)

  7. Build and run the application in Visual Studio .NET.