Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Determine if a simulink block exists

3,569 views
Skip to first unread message

John Quincy

unread,
May 4, 2010, 3:10:21 PM5/4/10
to
Hello,

I would like to be able to determine if a given simulink block exists, I have been trying to use find_system(block_name) however this returns an error if the block does not exists as opposed to an empty array. I guess I could use try catch on this but I would like to avoid that if possible. is there an easier way to do this?

John Quincy

unread,
May 5, 2010, 10:22:19 AM5/5/10
to
"John Quincy" <q.jo...@gmail.com> wrote in message <hrpret$ro1$1...@fred.mathworks.com>...

> Hello,
>
> I would like to be able to determine if a given simulink block exists, I have been trying to use find_system(block_name) however this returns an error if the block does not exists as opposed to an empty array. I guess I could use try catch on this but I would like to avoid that if possible. is there an easier way to do this?

Just to clarify I would like to easily determine if a simulink block referenced by name is in my model or not, is there a function to do this?

Klaus

unread,
Jan 12, 2012, 12:55:08 PM1/12/12
to
"John Quincy" <q.jo...@gmail.com> wrote in message <hrruur$o56$1...@fred.mathworks.com>...
Hey John,
just had the same Problem. It might be a bit too late for you but maybe someone else needs that info.

System = 'YourSystemName';
SysList = find_system(System,'BlockType','SubSystem')
than you find out if your Block is in the SysList.

Greetings Tom

Samuel Jones

unread,
Apr 3, 2016, 11:26:09 PM4/3/16
to
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.
0 new messages