I understand I am necroing this thread but as its the top google entry I thought I better update it with an alternative, more ideal, time-efficient method using the "getSimulinkBlockHandle()" function.
This is more ideal than Klaus' find_system method under the following situations:
# You know the path of the block to be found
# The system you are searching contains many subsystem blocks
The reason for this is because MATLAB/Simulink will just search for the specified path, rather than search the specified system ('System' in Klaus' example) for all subsystem blocks which then requires a strcmp analysis.
% If 'modelname/sysname' does not exist, handle will be -1. Otherwise it will be the block's handle.
handle = getSimulinkBlockHandle('modelname/sysname');
% Example usage:
if (getSimulinkBlockHandle('modelname/sysname') == -1)
... % block does not exist
else
... % block exists
end
When a relative path is needed, use find_system as per Klaus' solution. However you can also construct the block path using gcs, gcb, bdroot, etc. for variable paths as follows:
% relative to model root
blockpath = [bdroot(gcs) '/somesysinmodel/sys1']; % bdroot(gcs) is the model name.
% relative to current system
blockpath = [gcs '/somesysinsys/sys1'];
% relative to current block
blockpath = [gcb '/somesysinblk/sys1'];
% Usage:
if (getSimulinkBlockHandle(blockpath) == -1)
... % block does not exist
else
... % block exists
end
See
http://au.mathworks.com/help/simulink/slref/getsimulinkblockhandle.html for more information.