Borrar filtros
Borrar filtros

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

3 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Danny S
Danny S el 25 de Jun. de 2013
Editada: Danny S el 25 de Jun. de 2013
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().

Más respuestas (0)

Categorías

Más información sobre Block and Blockset Authoring en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by