Microsoft Visual C/C++ .NET compiler (version 7.0)
Microsoft Visual C/C++ compiler (version 6.0)
Watcom compiler (version 11.0)
UNIX compilers
All of these examples assume some knowledge of the compiler's debugging capabilities. Refer to your compiler documentation for further information on debugging files. The examples included in this Technical Note require sfundebug.mdl and timestwo.c.
Download these files and place them into a working directory.
Microsoft Visual C/C++ .NET
Open the Simulink model sfundebug.mdl.
To debug a MEX-file from within MATLAB, you must first compile the MEX-file with the -g option to the MEX function. The -g option builds the MEX-file with debugging symbols included. mex -g timestwo.cThis will create timestwo.mexw32. You should now simulate the model to make sure it executes properly.
Start the Microsoft Development Environment. Do not exit your MATLAB session.
From the Microsoft menu, select Tools -> Debug Process�
In the Process GUI that opens, select the MATLAB.exe process and click Attach
In the GUI that opens, select Native as the Type and click Ok. You should now be attached to the MATLAB process. You can now close the Process GUI if you wish.
Open the source files by selecting File -> Open->File� Set a breakpoint on the desired line of code by right-clicking on the line of code, and selecting Insert Breakpoint. If you have not run the model yet and the breakpoint shows up with a �?� in it, do not worry. The .mexw32 file will load when you run the model.
Start the simulation, and you should be running the S-function in the Microsoft Development Environment. For more information on the how to use the debugging features in the Microsoft environment, refer to the Microsoft Development Environment documentation.
Microsoft Visual C/C++ 6.0
Open the Simulink model sfundebug.mdl.
To debug a MEX-file from within MATLAB, you must first compile the MEX-file with the -g option to the MEX function. The -g option builds the MEX-file with debugging symbols included.mex -g timestwo.c
This will create timestwo.mexw32. You should now simulate the model to make sure it executes properly.
Exit your MATLAB session.
Open a DOS window (Start -> Programs -> Command Prompt), and change directories to the directory where your S-function and .mexw32 file are located. Then type:msdev timestwo.mexw32 This will start up the Microsoft Visual C/C++ compiler. This command will fail if msdev.exe is not on the system path. If you receive an error message when executing the msdev command, you will need to add the compiler directories to your system path. You should refer to the compiler documentation for more information on defining the system path for your compiler.
In the Microsoft environment, select Settings from the Project menu. In the dialog box that opens, select the Debug tab. In the edit box labeled Executable for debug session, enter the full path to where the MATLAB executable resides. For example, if you have MATLAB installed in a directory $MATLAB under Windows, you would enter $MATLAB\bin\win32\matlab.exe. The other edit boxes in this dialog box should remain empty.
Open the source files by selecting File -> Open. Set a breakpoint on the desired line of code by right-clicking with your mouse on the line of code, and selecting Insert/Remove Breakpoint.
From the Build menu, select Debug, and click Go. A dialog box will appear that warns you that the MATLAB executable (matlab.exe) does not have debugging information. This is expected, as we do not ship a debug version of MATLAB. Click OK to close this dialog box.
A new MATLAB session will start. You should be in the directory where your model and S-function are located. Open the model by typingsfundebug at the MATLAB prompt.
Start the simulation, and you should be running the S-function in the Microsoft Visual C/C++ environment. For more information on how to use the debugging features in the Microsoft environment, refer to the Microsoft Development Studio or Microsoft Visual Studio documentation.
Watcom 11.0
Open the Simulink model sfundebug.mdl.
To debug a MEX-file from within MATLAB, you must first compile the MEX-file with the -g option to the MEX function. The -g option builds the MEX-file with debugging symbols included. mex -g timestwo.c This will create timestwo.mexw32.
You should now simulate the model to make sure it executes properly.
Exit your MATLAB session.
Open up a DOS window (Start -> Programs -> Command Window), and type the following to open the Watcom compiler:Wdw This command will fail if Wdw.exe is not on the system path. If you receive an error message when executing the Wdw command, you will need to add the compiler directories to your system path. You should refer to the compiler documentation for more information on defining the system path for your compiler.
The Watcom Debugger starts and a New Program window opens. Type the full path to where MATLAB resides on your system in the Program Name field. For example:C:\matlab\bin\win32\matlab.exe Click OK to close this dialog box.
From the Break menu, select ON IMAGE LOAD and type the name of S-function in capital letters. For example:TIMESTWO Then select ADD and click OK. Note: You should only enter TIMESTWO. You do not need to enter the entire path to timestwo or timestwo.mexw32 .
From the RUN menu, click GO to start debugging your S-function.
When MATLAB starts, change directories to the location where you saved the model and the S-function. Note: MATLAB may start minimized.
Open the model and start the simulation.
Open the source file by double-clicking on timestwo in the MODULE window. This brings up the source code, timestwo.c, for setting breakpoints.
You should now be able to debug your S-function in the Watcom environment. For more information about how to debug in this environment, refer to the documentation for the Watcom compiler.
UNIX Compilers
Open the Simulink model sfundebug.mdl.
To debug a MEX-file from within MATLAB, you must first compile the MEX-file with the -g option to the MEX function. The -g option builds the MEX-file with debugging symbols included.mex -g timestwo.c You should now simulate the model to make sure it executes properly.
Exit your MATLAB session.
Start MATLAB with the debugger using the following command:matlab -D
Once the debugger has loaded, continue loading MATLAB by typing run at the debugger prompt.(dbx) run Running: matlab (process id 9375)
NOTE: If you are using MATLAB R12 or R12.1, use the following instead: (dbx) run -nojvm Running: matlab (process id 9375)
After MATLAB starts, enable debugging by typing the following at the MATLAB command prompt:dbmex on
Open the model and start the simulation. This will bring you into the debugger.
Set breakpoints in the source code:(dbx) stop in mdlOutputs
(2) stop in `timestwo.mexsol`timestwo.c`mdlOutputs
(dbx) file timestwo.c
Issue the cont command to continue.(dbx) cont
Once a breakpoint is hit, use the debugger routines to continue debugging the S-function.For more information about how to debug in this environment, refer to the documentation for the UNIX compiler.







