MATLAB: How to extract a block which is present in the subsystem under the mask of a library block from the command line

accessblockcommandextractlibrarylinemaskedsimulinksubsystem

For example, let us consider that the top-level library block is the ‘Discrete Transfer Function( with initial outputs)’ block in the Simulink Extras -> Additional Discrete library. I want to add the ‘Discrete State Space’ block inside the library block to a model. How do I do this using the command line?

Best Answer

Please execute the following steps:
1.Place the ‘Discrete Transfer Function( with initial outputs)’ block into a blank model called ‘test’;
2.‘Look Under Mask’ and then select the ‘Discrete State Space’ block;
3.Find out the ‘Block type’ of the ‘Discrete State Space’ block by typing the following in the MATLAB command window:
get_param( gcb , 'BlockType' )
This should produce the following answer:
ans =
DiscreteStateSpace
4.You need to know this information before hand to find the block using the following command:
blockHandle = find_system( 'test' ,...
'FindAll' , 'on' , ...
'LookUnderMasks' , 'all' , ...
'FollowLinks' , 'on' , ...
'blocktype' , 'DiscreteStateSpace' )
The ‘LookUnderMasks’ and ‘FollowLinks’ options are most important in this case since we want to access a block inside a library block which is a masked subsystem;
5.Then,
add_block( blockHandle , 'test/Copied Block' )
You might need to tweak the semantics of the FIND_SYSTEM command depending on your specific top-level library block. For example, you might need to substitute ‘blocktype’ with some other block parameter. For a list of block parameters, please refer to the following documentation link:
For more information on the FIND_SYSTEM command, please refer to the following documentation link:
Copying the top-level library block to a dummy model is a necessity because FIND_SYSTEM works only on systems and not on just library blocks. This is because the root needs to be loaded into memory. Since library blocks are not loaded into memory until they are placed in a model, we cannot use FIND_SYSTEM directly on them. The same concept would apply to any other method of acquiring the contents of a library block that is a subsystem.