Is it possible to write a script to automatically replace all S-Functions in a model with their counterpart Simulink Blocks?

I have a huge Simulink model which is filled with loads of S-Functions, and I'm looking to write some code to automatically replace them with Simulink blocks (i.e. the original blocks that the S-Functions replaced). There are too many to go through and do it manually. I was wondering if there's some way of calling the Simulink models?
I've tried using replace_block:
open ModelName
SFuns_loc = find_system('ModelName','BlockType','S-Function');
for s = 1:max(size(char(SFuns_loc)))
replace_block('ModelName','S-Function','Mux','noprompt');
end
So I can replace them with Muxes or bus creators etc (just as an example of how I'm using the replace_block function). However, what I need is a piece of code that will do the same thing as double-clicking on each S-Function and selecting "replace block with original Simulink Model".
Thanks for your advice in advance.
Ben

 Réponse acceptée

Try only
replace_block('ModelName','S-Function','Mux','noprompt');
It will replace all S-Function blocks in the model with Mux.
[EDITED]
I think replace_block() will not work here. You try with following steps
  1. Find all S-function blocks in the model using find_sustem().
  2. Get the position of s-function block using get_param().
  3. Delete the s-function block using delete_block().
  4. Copy the Subsystem from back-up model file using add_block() at the same position.
You can operate a loop to replace each S-function block one by one using above steps.

5 commentaires

Thanks for your answer, but I know it will do that already. What I want to know is if there is a way of replacing the S-Functions with their original Simulink Models instead of just a general mux (the mux is just there as an example of the type of how I was using the replace_block function). So, if you double-click on an S-Function in Simulink, you get the option "replace block" whereby the S-Function is replaced with the original Simulink Block that it replaced itself (convoluted I know!). Unfortunately there are too many S-Functions in my model to manually go through double-clicking and replacing them, so I need a script to do it.
What is
"original Simulink Models" & "original Simulink Block" ?
A block can not be replaced by model.
If you are asking to convert s-function into equivalent block diagram, then it is not possible.
Ah ok I see what you mean. Ok, here's a screenshot of what you get when you double-click on an S-Function:
https://picasaweb.google.com/lh/photo/3HgKUu7YME3sR17bxLm2btBX4mLCbm8rs6KYdwyJHGY?feat=directlink
(apologies for the crappy upload, needed to do it quickly!)
The point of an S-function is to create a dll file from a Matlab subsystem so that if you have a large model it can be run much more quickly. A reference to the original replaced subsystem is kept though, so you can restore it if you need to, and that's what I'm trying to do, just on a large scale in a model with about 3000 S-functions. Apologies for not being clear.
OK, think I got it.
So how you have backed-up your original subsystems ? Are they in the same model file or in different file ?
See some suggestion in edited part of my answer.
Ahh that's brilliant. Yes, I found the original model files and used add_block and set_param for the position and it's worked. Thank you very much!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by