Simulink loading- and simulation-procedure from Matlab script considering variant subsystems.

Hi there,
I'm currently wondering about when Simulink models Variant-Systems are evaluated when calling "load_system" to load a model inside a script.
My current approach is
function result = some_method( self )
h = load_system('foo');
hws = get_param (model, 'modelworkspace');
hws.assignin('sim_config', self);
result = sim('foo', ...);
end
Now I've got multiple problems with that.
- How does the "sim" command actually know the "h" model handle and use that instance, since I only pass the model name? - Is it not too late to define the workspace variables afer the load_system call, especially considering variant subsystems? - Is "load_system + sim" the intended pattern for what I'm doing?
Kind Regards, Michael

5 Comments

What I'm ideally trying to do is to have a variant subsystem change its variant based on the CLASS TYPE of a variable (that class provides all the parameters for the given variant).
Since I can't quite understand the rationale of the Variant system, especially the string conditions etc. and why there need to be Variant-Objects for each Variant of a Variant-Subsystem, this is what I'm currently trying to do:
Matlab script to run the model:
model = load_system('variant_test_model');
hws = get_param (model, 'modelworkspace');
myval = foo_class();
hws.assignin('obj', myval );
X = sim('variant_test_model', 'SolverType', 'Fixed-step', 'Solver', 'ode4', 'FixedStep', '1e-5', ...
'StartTime', '0', 'StopTime', '1', 'SaveOutput','on','OutputSaveName','outputs' );
Model's Init Function:
variant_variable = 1;
variant1 = Simulink.Variant('variant_variable == 1');
variant2 = Simulink.Variant('variant_variable == 2');
Model's Start Function:
if( strcmp( class( obj ), 'foo_class' ) == 1 )
variant_variable = 1;
elseif( strcmp( class( obj ), 'bar_class' ) == 1 )
variant_variable = 2;
end
I would have liked it to be more centered in the Variant-Subsystem instead of the model's properties, but the Variant-Design of Simulink seems not to be capable of that.
Now my problem is, I get the error message "Undefined function or variable 'obj'." during the StartFcn process. I can only assume that the sim command actually reloads the model and therefore uses a different workspace than the one I implanted "obj" into.
Now I would be grateful if somebody could help me get this working and even more if someone can come up with a cleaner design of what I'm trying to do.
Kind Regards, Michael
Okay, instead of the "load_system, get workspace, ws.assignin( ... )" procedure, "assignin('base', 'value', some)" seems to do the trick.
Which rises a new question: How can I run a simulation of a model which I loaded and configured with a call to load_system?
And the remaining question is: Is there a more elegant solution to the callback-hack for the variant selection by object class type?
I tried to add a mask and a hidden "variant_variable" parameter, so I don't have to have the variant condition variable inside the base workspace (which would collide with copied blocks obviously), but it turns out that the Simulink.Variant string parsing stuff can only access the base workspace. I can't help but think that Simulink's variant design is inherently broken beyond repair... getting angry at it :(
My next attempt, which seems to get me halfway to intended behaviour is
if( strcmp( class( cfg ), 'foo_class' ) == 1 )
set_param(gcb, 'OverrideUsingVariant', 'foo == 1');
elseif( strcmp( class( cfg ), 'bar_class' ) == 1 )
set_param(gcb, 'OverrideUsingVariant', 'foo == 2');
end
inside the Mask Initializer. However, if the model is not opened, but merely opened by "sim", the mask initializer seems to run too late, yielding the error message
"Block 'variant_test_model/Variant Subsystem' attempted to change the active variant during simulation. The active
variant must be configured before the simulation is started."
Even the Block's StartFcn-callback comes to late for this call. Any suggestions?
Even more experiments led to
set_param('variant_test_model/Variant Subsystem', 'OverrideUsingVariant', 'foo == 1');
with which I can reliably change the Variant in use from my matlab script/function. Unfortunately, I was not able to find a model- (or preferrably block-) callback that gets called for each sim-call early enough to change the variant inside the model :-(

Sign in to comment.

Answers (0)

Categories

Products

Asked:

on 20 Feb 2016

Edited:

on 20 Feb 2016

Community Treasure Hunt

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

Start Hunting!