User Tools

Site Tools


software:matlab

Matlab and Simulink tips and tricks

Choosing the compiler in MATLAB

To choose the compiler in MATLAB, type:

>>mex -setup

in MATLAB Command Window and let MATLAB locate installed compilers. Then choose one of the listed compilers. If you want to use Visual Studio to build your MEX files, choose Visual C++ Compiler.

Setting up a Visual Studio project for building and debugging S-functions

To set up a Visual Studio project for building and debugging “ordinary” MEX functions, see this article on The Code Project: Matlab 7.1+ and Visual Studio 2005. For Simulink S-functions, read on. The instructions have been tested with Visual Studio 9 (2008) and Matlab R2009a, but should work with any similar setup.

Building S-functions with Visual Studio

1. Create a new Visual Studio project

As project type, choose Win32 Console Application. In the project creation Wizard, under Application Settings select

  • Application type: Console Application
  • Additional options: Empty project

2. Add files to your project

  1. Your own files. Sample S-function code can be found in <MATLABROOT>\simulink\src (timestwo.c is a most elementary example and sfuntmpl_basic.c is a template providing all the necessary code structure)
  2. The resource script mexversion.rc from <MATLABROOT>\extern\include
  3. Create a new .def file and add it to the project. The .def file should contain only the following two lines:
LIBRARY DefFileName
EXPORTS mexFunction

3. Set project properties appropriately

  1. General→Configuration type: Dynamic Library (.dll)
  2. C/C++→General→Additional Include Directories: add “<MATLABROOT>\extern\include”;“<MATLABROOT>\simulink\include”
  3. C/C++→Preprocessor→Preprocessor Definitions: add _CRT_SECURE_NO_WARNINGS;_USRDLL;MATLAB_MEX_FILE
  4. Linker→General→Output File: Change extension to .mexw32 (for 32-bit MATLAB) or .mexw64 (for 64-bit MATLAB)
  5. Linker→General→Additional Library Directories: add “<MATLABROOT>\extern\lib\win64\microsoft” (change win64 to win32 for 32-bit MATLAB)
  6. Linker→Input→Additional Dependencies: add libmx.lib libmex.lib libmat.lib
  7. Linker→Input→Module Definition File: add .def file

Now you should be able to build your project and get the .mexw64 (or .mexw32) file.

Using the Visual Studio debugger with S-functions

  1. Set breakpoints in your code.
  2. Go to Tools→Attach to process…
  3. From the process list, select MATLAB.exe
  4. Run the model containing your S-function

For details, see links in the Resources section.

Tips and tricks: Troubleshooting MEX-file errors

When trying to run the S-function block in Simulink, you might receive error report of this type:

Error while obtaining sizes from MEX S-function <function name> in <function folder>
MATLAB error message: Invalid MEX-file <MEX-file path> : the specified module could not be found

This error is, in most cases, caused by missing one or more dynamic-link libraires (-.dll) and can be fixed by installing Visual C++ runtime libraries.

If problem persists, there are two possible reasons:

1.If you use Visual Studio 2008 and set up preprocessor definition _DEBUG, problem is that VS2008 doesn't install debug version of dlls

Solution: Install Visual Studio 2008 Service Pack 1

2.Your MEX-file has unresolved dependencies

Solution: You need to check dependencies of the MEX-file. You can use Dependency Walker for such purpose. Locate missing dlls and add path to the folder containing the dlls to DOS Path of Windows OS.

Note: Dependency Walker sometimes can't find the files (or dlls) that MATLAB can, such as libmex.dll or libmx.dll. Any error messages by Dependecy Walker about missing these files you can ignore, because MATLAB will find them (the same applies to dlls associated with Visual C++ runtime libraries, especially debug versions of dlls). Any other dependency error reported must be handled (hint: these errors often occur when accesing drivers of I/O devices on the computer).

Resources

software/matlab.txt · Last modified: 2017/05/12 09:29 (external edit)