MATLAB: Do I receive the error “Invalid MEX-file” in Simulink 5.0 (R13) only on the first run after the compiling of the S-function

errorfileinvalidmexmex files-functionsimulink

When compiling, I put my H-files and C-files in different directories, as well as my .DLLs in a separate directory. After a successful compilation of my S-function, I run my model and it stops execution at the S-function and gives me the following error:
Error while obtaining sizes from MEX S-function 'S_func' in block 'Block1/S_func.'
MATLAB error message: Invalid MEX-file.
It runs without error every time thereafter. This error occurs everytime on the first run when I just open the model, recompile the S-function, or update the diagram (Ctrl+D).
Also, I do not get the error if I put all of my files into a single directory.

Best Answer

This occurs because Windows cannot locate the DLL-files that are encapsulated in a linked LIB-file. Make sure that the DLL-files called by your included LIB-files are on the Windows path. To do this in Windows, click on the Start button. Then click on:
Control Panel -> System -> Advanced (tab) -> Environment Variables (button)
In the "System variables" frame, click on "Path", then click on the "Edit..." Add the directory where the LIB-file exists to the beginning of the "Variable Value" and click "OK".