How to use C s-functions to save Simulink model signals to .mat files

I have a Simulink model that I compile into a DLL using the Simulink Coder toolbox (this DLL is used in another application, which I have no control over). This model contains To File blocks, which save the values of some signals into *.mat files. This work fine for one simulation. However, when using the DLL in a loop inside the application, the *.mat files are getting crushed by the succeeding ones. Would it be possible to have a C s-function that creates the *.mat files (instead of the To File block) and saves them with a different names, or moves them into a specific folder? I looked at this MATLAB page, but didn't find any example on how to save *.mat files in a C s-function.