In the Matlab of windows, write some pits in C language sfunction

Source: Internet
Author: User

1. Test environment:
    • Operating system: Windows ten, 64bit
    • Compiler: MINGW64
      • 32-bit system please download the corresponding 32-bit version
    • matlab2017a
      • It will be slightly different after 2017a
2. Configure the environment and process:
    1. Download compiler MinGW64, for 64-bit systems, you need to download an installer, you can install the corresponding version of the GCC compiler, I choose gcc-4.9.4, in the test environment can run properly

      1. Download down the installer is mingw-w64-install, installation attention to choose 64-bit, while the installation is finished there is a hole: space .
      2. When you change the default installation path of the downloader "C:\Program files\mingw-w64\x86_64-4.9.4-win32-seh-rt_v5-rev0\mingw64" contains spaces, MATLAB does not accurately identify So you can move the mingw64 to a path without spaces, such as "C:\mingw64".
    2. Verify that:

      Enter the following as shown:

      This way the MinGW compiler is configured.

3. Quick compile:
setenv(‘MW_MINGW64_LOC‘,‘C:\mingw64‘)myFunctionName = ‘SFunctionName‘def = legacy_code(‘initialize‘);def.SFunctionName = myFunctionName ;% 注意,输入和输出的变量的名称只能是u1,u2...和y1,y2...def.OutputFcnSpec = ‘double y1 = add_2(double u1, double u2)‘;% 设置用到的头文件或者源文件def.HeaderFiles = {‘add_2.h‘};def.SourceFiles = {‘add_2.c}def.InitializeConditionsFcnSpec = ‘buffer_init()‘;legacy_code(‘sfcn_cmex_generate‘, def);legacy_code(‘compile‘, def) ;

The above script can be quickly compiled in MATLAB, and generate the files available in Simulink, add the S-function module in Simulink, and set the name to "Add_2".

ADD_2.C can directly write a two number of the program to verify the addition of a bit; Add_2.h write a function declaration is also possible.

// add_2.c#include "add_2.h"double add_2(double a, double b){    return a + b ;}// add_2.h#ifndef ADD_2_H#define ADD_2_Hdouble add_2(double a, double b);#endif
4. Reference
    1. Mathwors a description of the space
    2. matlab2017b and how to set it afterwards
    3. Thanks for the video commentary of India's little brother

Sfunction some pits in C in the Matlab of Windows

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.