MATLAB: How to find all the blocks that are connected to a startblock

connectionslineslinkssimpowersystems

I have a simulink model of a power system and I am trying to write a program that works from a start block and outputs all the connected blocks. The system has some connections that junction. The intent is to then re allocate the connected blocks into current blocks and find all their connected blocks until one of the connected blocks is the end block then end the script. PortHandles is only outputting in Outport, LConn and RConn but when i tried getfullname(Outport) it said invalid simulink object handle. I got the same error with DstBlock/DstPort in PortConnectivity and LineHandles.
%populates blocks from system
nodes = find_system(gcs,'block','Node');
TL = find_system(gcs,'Block','Transmission Line');
Fuse = find_system(gcs,'block','Fuse');
Relay = find_system(gcs,'block','Relay');
Load = find_system(gcs,'block','Load');
Switch = find_system(gcs,'block','Switch');
PC = Fuse;
PC(size(PC,1) + 1:size(PC,1) + size(Relay,1)) = Relay;
%PC(size(PC,1) + 1:size(PC,1) + size(Load,1)) = Load
PC(size(PC,1) + 1:size(PC,1) + size(Switch,1)) = Switch;
allBlocks.blocks = nodes;
allBlocks.blocks(size(allBlocks.blocks,1) + 1:size(allBlocks.blocks,1) + size(TL,1)) = TL;
allBlocks.blocks(size(allBlocks.blocks,1) + 1:size(allBlocks.blocks,1) + size(Load,1)) = Load;
allBlocks.blocks(size(allBlocks.blocks,1) + 1:size(allBlocks.blocks,1) + size(PC,1)) = PC;
%%User Input and Error Checking
% While loops ensure that the blocks actually exist in the system.
startBlock = cell(1); endBlock = cell(1);
%INPUTBOX COMMENTED OUT FOR CODING
in = inputdlg({'Start Block','End Block'},'Inputs');
startBlock{1} =in{1,1};%'OLDPLANTBUS'; %input('What is the name of the block you would like to start with? ','s');
endBlock{1} =in{2,1};%'SL1575'; % input('What is the name of the block you would like to end with? ','s');
startBlock{1} = [gcs, '/', startBlock{1}];
endBlock{1} = [gcs, '/', endBlock{1}];
while getSimulinkBlockHandle(startBlock{1}) == -1
startBlock{1} = input('Your Start Block does not exist in the system, what is the name of the block you would like to start with? ','s');
end
while getSimulinkBlockHandle(endBlock{1}) == -1
endBlock{1} = input('Your End Block does not exist in the system, what is the name of the block you would like to start with? ','s');
end
%building connections
currentBlocks.blocks = startBlock;
currentBlocks.porthan = get_param(currentBlocks.blocks, 'PortHandles');
currentBlocks.pcon = get_param(currentBlocks.blocks, 'PortConnectivity');
currentBlocks.linehan = get_param(currentBlocks.blocks, 'LineHandles');
allBlocks.porthan = get_param(allBlocks.blocks, 'PortHandles');
allBlocks.pcon = get_param(allBlocks.blocks, 'PortConnectivity');
for i = 1:size(currentBlocks.linehan);
currentBlocks.lhandles(i, 1:numel(currentBlocks.linehan{i, 1}.LConn)) = currentBlocks.linehan{i, 1}.LConn;
currentBlocks.lhandles(i, end + 1 : end + numel(currentBlocks.linehan{i, 1}.RConn)) = currentBlocks.linehan{i, 1}.RConn;
end
indexA = getfullname(currentBlocks.lhandles);

Best Answer

Most likely, you see the error because some of your blocks might not have lines connected to them. So, the LConn or RConn's values may be -1. In this case, getfullname will throw an error because -1 is an indicator for an invalid Simulink object handle.
So, make sure that the input array to the getfullname function does not have -1 in them before you execute the function, and it should work as expected.
Related Question