How do I programatically initialize a M-S-Function in a Library?

3 次查看(过去 30 天)
I am trying something which is somewhat complicated, so please bear with me!
For our customers, we wish to supply a generic M-File S-Function and a script that will generate a Simulink Library containing a S-Function block (using the M-file) with parameters based on their input.
The library and the Simulink blocks it contains are generated programatically (using new_system, add_block, etc) by the generation script.
My M-S-Function defines the number of input ports and output ports in the setup function, based on Dialog parameters.
i.e., in my_sfcn.m:
function my_sfcn(block)
setup(block);
%endfunction
function setup(block)
% parameters:
% 1 - names of output ports (cell array of strings)
% 2 - names of input ports (cell array of strings)
block.NumDialogParams = 2;
out_names = block.DialogPrm(1).Data;
in_names = block.DialogPrm(2).Data;
% Set up number of input and output ports
block.NumInputPorts = length(in_names);
block.NumOutputPorts = length(out_names);
% ... rest of setup ...
I am then (in the generation script), creating the Library as follows:
new_system('mylib', 'Library')
add_block('built-in/M-S-Function', 'mylib/mysfcn_block',
'FunctionName', 'my_sfcn', 'Parameters', my_params)
Where `my_params` is the parameters of the S-Function (output port names and input port names)
The problem now is if I attempt to connect the ports that I know are there, any beyond the first will cause an error:
add_block('built-in/Inport', 'mylib/in1')
add_line('mylib', 'in1/1', 'mysfcn_block/1')
add_block('built-in/Inport', 'mylib/in2')
add_line('mylib', 'in2/1', 'mysfcn_block/2')
Invalid Simulink object name: mysfcn_block/2.
This is because the M-S-Function has not been initialized (setup has not been called yet) so the number of ports has not been correctly updated in the added Simulink block.
Is there a programatical way that I can initialise the M-S-Function block and call setup()?
Or is there a parameter I can set that will make the block have the correct number of input and output ports? (something equivalent to block.NumInputPorts, but able to be set via add_block() or set_param())
I can't compile the model (using the technique described on http://www.mathworks.com.au/help/simulink/slref/model_cmd.html ) as this is taking place inside a Library and not a Model, so I am hoping there is another way to make the added S-Function block have the correct number of ports.
Note that I do not know how many ports there will be before the Library generation script is run, my customer will supply this in a configuration file and they will run the generation script.
I am using MATLAB R2010bSP1 and R2011b on Windows and Linux

采纳的回答

Danny S
Danny S 2013-6-25
编辑:Danny S 2013-6-25
I've managed to discover an answer to my own question, thanks to: http://www.mathworks.com.au/help/simulink/sfg/input-and-output-ports.html#f4-90846
Essentially, what I need to do is:
set_param(blockname,'MaskSelfModifiable','on')
And with this parameter set, the S-Function block updates its number of ports as per specified in setup().

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Block and Blockset Authoring 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by