How can I create a bus for Simulink from a Matlab function?

18 views (last 30 days)
I have a function (myFunc) where I call a Simulink model that uses bus structures.
Before calling the model from myFunc, I used the following commands to create the bus and just run the model:
% References
busInfo2=Simulink.Bus.createObject(REF);
RefVectorBus = eval(busInfo2.busName);
clear slBus1; %(slBus1 is the default name. As I changed it to RefVectorBus, I can clear slBus1)
% Controller Configuration
busInfo3=Simulink.Bus.createObject(controllerConfig);
controllerConfigBus = eval(busInfo3.busName);
clear slBus1;
But now that I have this:
output = myFunc (input)
//declare model parameters (as I use current workspace)
//initialize bus:
% References
busInfo2=Simulink.Bus.createObject(REF);
RefVectorBus = eval(busInfo2.busName);
clear slBus1;
% Controller Configuration
busInfo3=Simulink.Bus.createObject(controllerConfig);
controllerConfigBus = eval(busInfo3.busName);
clear slBus1;
//call the model (USING CURRENT WORKSPACE)
SimulationOut = sim('myModel','SaveOutput','on', 'SrcWorkspace','current');
...
end (myFunc)
The problem is that Matlab doesn't allow me to create the bus ("Undefined function or variable 'slBus1'"). Is there a problem with the current workspace? Is matlab looking for the bus info in the base workspace?
I need the current workspace because the myFunc is a fitness function handle for a genetic algorithm and some of the Model parameters are beig estimated so I have to define them INSIDE myFunc and not in the base Workspace.
How can I solve this? Thanks to you

Answers (1)

Josep Virgili LLop
Josep Virgili LLop on 29 Mar 2017
Edited: Josep Virgili LLop on 29 Mar 2017
When using `Simulink.Bus.createObject` Matlab assigns the bus object to the base workspace and, as far as I know, this behavior can't be changed.
If you want to recover the Simulink bus in the current workspace use `evalin`
busInfo=Simulink.Bus.createObject(struct);
slBus = evalin('base',busInfo.busName);
See the evalin docs.

Community Treasure Hunt

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

Start Hunting!