Simulink: replace existing subsystem with another one - simulink

How do i replace an existing SubSystem in a given model with another SubSystem from another model.
I tried commands like:
Simulink.SubSystem.copyContentsToBlockDiagram(subsys, bdiag)
replace_block
but nothing worked (maybe I used it wrong).
Remarks:
I am using matlab 2011b.

Related

How to use structure as parmeter for simulink subsystem and use fields inside as block parameters?

I have created a subsystem in Simulink. I would like this subsystem to have a single parameter, which is supposed to be a Matlab structure. Inside the subsystem, I would then like parameters of some of the blocks to be the fields of this structure.
So for example, imagine the subsystem has a single parmeter sys_inputs, then inside the subsystem I have two Constant blocks and the value for the first Constant block should be sys_inputs.Constant1, and the other should be sys_inputs.Constant2.
Is this possible, and if so, how exactly?
I'm finding the Matlab documentation on passing mask parameters to subsystem internal blocks a bit obtuse.
The purpose is so that the user only has to provide a single parameter to the subsystem instead of changing many parameters, when this input usually comes pre-packaged as a structure.
I discovered the answer myself.
First create the subsystem
Right click and 'create mask'
Edit mask parameters
Add an 'edit' parameter. The 'Name' of this parameter will be the variable name you can use inside your subsystem, e.g. sys_inputs
Inside the subsystem, you can use the sys_inputs parameter as though it was a workspace variable, so you can enter things like sys_inputs.Constant1 in the subsystem components parameters

Run Simulink from Matlab function

I am running Simulink using FastRestart, as I need to start and stop the simulation multiple times changing parameters. When I run Simulink from the main script, there are no problems. However, as soon as I make the script a function so that I can run it for different input data, I get an error that is clearly related to Simulink not seeing the Matlab workspace within the function.
To be more precise, say sfile is my Simulink file, then I run the following lines AFTER having initialized all variables I need in structures in Matlab:
load_system(sfile);
set_param(sfile,'FastRestart','on');
set_param(sfile,'SaveFinalState','on');
set_param(sfile,'SaveCompleteFinalSimState','on');
set_param(sfile,'SimulationCommand','update');
At the last line, I get the error that Simulink does not recognize mdl.tStep (which is the time step), as mdl is not a recognized structure. In fact, it is and if I run Simulink from the main script everything is fine.
Now, in the past, I would have used
options = simset('SrcWorkspace','current');
However, an expert I know has advised me against simset (as it may get deprecated in the future) and encouraged me to use set_param instead. I have
looked up the options for set_param on-line, but I could not find the setting for the Matlab workspace.
Any help would be greatly appreciated. Thank you in advance!
In many instances it is better to use the Model Workspace rather than the Base Workspace:
hws = get_param(model, 'modelworkspace');
hws.assignin('mdl',mdl);
At least be aware that this option exists.
A solution to your problem might be to use the assignin-function to all the variable whose value you want to pass to simulink in your matlab base workspace. To do so just use
assignin('base','mdl',mdl)
Having the variable in your base workspace should allow simulink to see it.

Plant box in Simulink

I am looking for a box in simulink that is "plant".
I cannot seem to find it in the search option of simulink.
Like the one in the next pic:
That's a subsystem that the person who created the model made, it's not a standard Simulink block. You need to look inside the subsystem to see how it's constructed. See Create a Subsystem in the documentation for more details.

Variable subsystem implementation in Simulink 2007a

I've seen that it's already implemented in Matlab R2013 in the form of Variant Subsystems, but budget and convenience don't show the upgrade necessary yet:
I am seeking a subsystem in which a concrete implementation can be selected prior to running the simulation, in Matlab R2007a.
A bunch of enabled subsystems along with a switch block connected to a masked variable would do the trick, however the whole family of selectable implementations must coexist inside the "container" subsystem.
Any workaround, other than upgrading to R2013?
Thank you.
I have come up with the following workaround.
1- Include all the possible implementations in a Library
2- Create a Configurable Subsystem block in the Library and edit it to include all the desired implementations
3- Right clicking in a Configurable Subsystem instance will show the "Block Choice" option where the desired implementation can be chosen.
Regardless of differences that may exist with respect to the Variant Subsystem solution when it comes to code generation, RT targets etc..., this solution works for me.

In simulink, programmatically create a variant subsystem

I want to use scripting to create and define a variant subsystem in simulink.
I am able to create the variant subsystem and add subsystems within it using the add_block but cannot find the appropriate property that defines the variant subsystem by using the get_param command.
The way I graphically configure a variant subsystem is by right clicking the block and going to Block Parameters and then adding the variant control conditions for each subsystem.
I want to do the same thing but from an m-file so that I can create multiple blocks programmatically.
I checked the documentation and google search but couldn't find anything.
Thanks in advance.
Based on a comment by #Praetorian, I opened the model in a text editor and found the parameter line where the variant condition was defined.
I found that the variant conditions are stored in the subsystems within the variant subsystem and not within the top-level variant subsystem.
So you store the condition within each subsystem's "VariantControl" parameter.
Example:
set_param([variantSys '/' sys],'VariantControl','a==1') where sys is a subsystem within the variant subsystem variantSys.
The variant control should be written on the Callbacks of the Model. In order to put the simulink variant controls available to the model, the variables should be created in the workspace. For this to happen you need to put your Sys = Simulink.Variant(Mode== value) etc on the PreLoadFcn.
Check access PreLoadFcn and put the formulas in there then your table will be automatically filled.
If you do not mind can you tell me how you created the variant subsystem?
Good luck