when using libinfo on my system it does not return values for all the blocks...

8 views (last 30 days)
I want to pass one of my functions the name of a block in a current system and the function will return code to insert that block from scratch. Here is the setup:
>> path = 'untitled';
>> topLevel(path);
>> open_system(path);
>> %%Populate System/subSystem
% Constant - c1
pos = [20 100 50 130];
add_block('built-in/Constant',[path '/c1'],'MakeNameUnique','on',...
'position',pos,'value','1','showname','off');
% Clock - clk
pos = location([-0.083333,0.500000],...
'ref',[path '/c1'],'w',20.000000,'h',20.000000);
add_block('built-in/Clock',[path '/clk'],'MakeNameUnique','on',...
'position',pos,'showname','off');
% Constant - c2
pos = location([0.083333,0.500000],...
'ref',[path '/clk'],'w',30.000000,'h',30.000000);
add_block('built-in/Constant',[path '/c2'],'MakeNameUnique','on',...
'position',pos,'value','0','showname','off');
% Switch - switch
pos = location([1.125000,0.500000],...
'ref',[path '/c1'],'w',35.000000,'h',88.000000);
add_block('built-in/switch',[path '/switch'],'MakeNameUnique','on',...
'position',pos,'Threshold','is.Ton','SampleTime','is.Ts',...
'Criteria','u2 >= Threshold','ShowName','off');
% Simulink-PS Converter - sps1
pos = location([1.291667,0.500000],...
'ref',[path '/switch'],'w',30.000000,'h',32.000000);
add_block('nesl_utility/Simulink-PS Converter',[path '/sps1'],...
'MakeNameUnique','on','position',pos,'ShowName','off');
This uses a function topLevel to build a system and open the necessary libraries. The remaining code will open the system with 5 blocks in it. The location function is just my way setting the position parameter of that block. It will make the origin of blk2 some distance x and y from blk1 and give it a width and height.
Now that I have a set model...
I want to find the parameter 'ReferenceBlock' of all the blocks. get_param does not show a value for some of the blocks. Example
>> get_param([path '/c1'],'ReferenceBlock')
ans =
''
>> get_param([path '/sps1'],'ReferenceBlock')
ans =
nesl_utility/Simulink-PS
Converter
So then I tried libinfo and a custom function to look through the structure for the block by name and pull out its parameter for 'ReferenceBlock'.
%%Find blk2 Reference library
strArray = libinfo(path);
blk2Lib = findBlock([path '/' blk2],strArray);
The function findBlock uses this code.
function refBlock=findBlock(blockName,stringArray)
[Nr,Nc]=size(stringArray);
refBlock='';
for(n=1:Nr)
x=strcmpi(blockName,stringArray(n).Block);
if(x==1)
refBlock_temp = stringArray(n).ReferenceBlock;
newline = sprintf('\n');
refBlock = strrep(refBlock_temp,newline,' ');
break;
else
refBlock = sprintf('%%FIND LIBRARY PATH%%');
end
end
When I attempt to use the function on the blocks in the system it will only find the reference for some of the blocks. If you look at the strings below one of them is not finding a 'ReferenceBlock' and outputs '%FIND LIBRARY PATH%'. This means libinfo does not have the parameter 'ReferenceBlock' for all blocks.
>> posRef = posReference(path, 'switch', 'sps1')
posRef =
% SubSystem - sps1
pos = location([1.291667,0.500000],...
'ref',[path '/switch'],'w',30.000000,'h',32.000000);
add_block('nesl_utility/Simulink-PS Converter',[path '/sps1'],...
'position',pos,'showname','off','BlockRotation',0,...
'Orientation','right');
>> posRef = posReference(path, 'c1', 'switch')
posRef =
% Switch - switch
pos = location([1.125000,0.500000],...
'ref',[path '/c1'],'w',35.000000,'h',88.000000);
add_block('%FIND LIBRARY PATH%',[path '/switch'],...
'position',pos,'showname','off','BlockRotation',0,...
'Orientation','right');
TL;DR libinfo does not find the parameter 'ReferenceBlock' for all blocks in a system.
How can i find out the parameter 'ReferenceBlock' for a given block so i can write the proper code to input into add_block()???
Thanks in Advance,
Pat

Answers (1)

Kaustubha Govind
Kaustubha Govind on 20 Jul 2012
Blocks from the Simulink 'built-in' library are the ones that do not have a 'ReferenceBlock' parameter. For such blocks, you may look at the 'BlockType' parameter instead. If it is not 'Subsystem' or 'S-Function', it should be one of the built-in blocks.

Categories

Find more on Simulation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!