Main Content

Generate Standalone Executable And Interact With It Using UDP

This example shows how to generate a standalone executable for streaming statistics using MATLAB Coder™ and tune the generated executable using a user interface (UI) that is running in MATLAB (TM).

Introduction

Most algorithms in DSP System Toolbox™ support C code generation using MATLAB Coder.

One of the options of MATLAB Coder is to generate a standalone executable that can be run outside of the MATLAB environment by launching the executable directly from a terminal or command prompt.

For algorithms that are tunable, it is desirable to interact with the algorithm at run-time using a UI. One way to achieve this is by sending/receiving information via UDP.

This example uses UDP to exchange between MATLAB and a generated standalone executable at run-time. The variance, bias, and exponential weighting values are sent from MATLAB to the executable. The actual random signal along with mean, RMS, and variance estimates are sent from the standalone executable back to MATLAB for plotting.

Example Architecture

该示例的架构由两个主要部分组成:

  1. streamingStatsCodegenExampleApp:一个MATLAB函数,可创建用户界面(UI)来更改差异,偏差和指数加权值。此功能还绘制了从独立可执行文件中接收到的数据。

  2. HelperStreamingStatsEXEProcessing:这是生成独立可执行文件的函数。此函数会生成给定偏差和方差的随机信号,并计算均值,RM和这种信号的方差估计。噪声信号以及计算的统计信息通过UDP发送以绘制(或任何进一步处理)。在模拟过程中的任何时候,它也可以响应MATLAB UI滑块的变化。

Generating Code and Building an Executable File

You can use MATLAB Coder to generate readable and standalone C-code from the streaming statistics algorithm code. Because UDP is used, there are additional dependencies for the generated code and executable file. These are available in the /bin directory of your MATLAB installation.

Running the scriptHelperStreamingStatsGenerateEXE将调用MATLAB aut编码器吗omatically generate C-code and a standalone executable from the algorithm code present inHelperStreamingStatsEXEProcessing.

Running the Example

Once you have generated the executable, run the functionstreamingStatsCodegenExampleAppto launch the executable and a user interface (UI) designed to interact with the simulation. The UI allows you to tune parameters and the results are reflected in the simulation instantly. For example, moving the slider for the 'Variance' while the simulation is running, will affect the noise signal along with the RMS and variance estimates that are plotted.

UI上还有两个按钮 - “暂停仿真”按钮将保存模拟,直到再次按下它为止。可以通过单击“停止仿真”按钮来终止模拟。

Threading

The standalone executable is executed as a separate process. This means that the graphics can run in parallel with the statistics computation. This can be an attractive approach for high performance computations involving graphics.

Manually Invoking the Executable

In lieu of using thesystem命令从MATLAB内部启动可执行文件,可以从终端或命令提示符手动启动可执行文件。由于此可执行文件包括UDP调用,因此必须将DLL在适当行为的路径上。看How To Run a Generated Executable Outside MATLABfor more information.