Thursday, January 16, 2020

Using workspace variables in C++ Mex functions

When you have C or C++ files that you would like to use in your Simulink model, you have to interface them with a mex file. Variables in Matlab workspace can be used inside C++ mex functions. Example m file and mex function demonstrating scalar, vector, matrix, string and structure usage:

When you run mexDriver.m, you will get an output similar to this:


Tuesday, January 7, 2020

Comparing two models

When you have an mdl/slx model, to compare it with another version of the same model:
  1. Open model in Simulink.
  2. Go to Analysis menu, click Compare Simulink XML Files...
  3. Select the model file you want to compare with the opened one.
  4. Simulink will open a window displaying the differences. When you click on a line, it will display both models, highlighting the differences.

Monday, January 6, 2020

File closing bug

Recently, I saw that when I ran a simulink model multiple times in a row from a script with sim() command, the model would throw an error at the 509th time with a message indicating that it could not find a file, i.e. an fopen() operation returned null. The curious thing is that the file exists, otherwise it would not be able to run the previous 508 times.

After one day of debugging, I saw that I forgot to close a file in another unrelated s-function. I guess after a certain number of unclosed file pointers, fopen() commands start to fail. This reminded me of an old heap corruption bug that took me months to find an fix.