Hi Fernando,
Although I didn't see your model and so I couldn't find the reason of the problem, I described the tips for this problem bellow. I hope this will help for you.
1. Set sample time of MATLAB function
In block parameters dialog(right click the block and select "Block Parameter") , you can set sample time for this block. It may be necessary to place Rate transition blocks before and after the range where the sample time was changed. (Please see the URL below for details)
https://jp.mathworks.com/help/rtw/ug/handle-rate-transitions.html
2. Using local solver for simscape
Using Solver Configuration block you can use local solver for simscape. It enable you to use different solver to simulate simulink model and simscape model individually.
3. Profiling the reason of stack
To profile the reason of stack, you can use "Solver Profiler". Please see the picture how to start this app. (Please see the URL below for details)
https://jp.mathworks.com/help/simulink/ug/examine-solver-behavior-using-solver-profiler.html
Please be careful that there is not Algebra loop in your model. If there is, that can make slower the simulation speed. In another case, using stiff solva might improve this problem (ode23t or something) because the model may be stiff due to the mixture of simulink model and simscape model.
I hope this will help.