Well, I donβt know how to do this from a simple function, but itβs really convenient to do it from inside a class (method) function. It works great with version 2009b.
Put the code in the Test.m file:
classdef Test < handle properties mdl % Default input signal t = [0 1 1 2]' u = [0 0 1 1]' end methods function this = Test(mdl) % Constructor this.mdl = mdl; end function sim(this) % Load model load_system(this.mdl); % Prepare model configuration conf = getActiveConfigSet(this.mdl); cs = conf.copy(); set_param(cs, 'StopTime', '4'); set_param(cs, 'LoadExternalInput', 'on'); set_param(cs, 'ExternalInput', '[test.t test.u]'); % <-- 1 % Run simulation simout = sim(this.mdl, cs); % Plot results tout = simout.find('tout'); yout = simout.find('yout'); plot(tout, yout(:,1), 'b--'); end end end
Then just:
>> test = Test('TestSim'); >> test.sim();
What's happening? You create an object test that has certain fields t and u. Then, in the sim () method, you tell Simulink to search for the input "[test.t test.u]". Both Simulink and the sim () method have access to these variables (I believe this is the most important).
OK, it still has a big flaw, marked by number 1. You must know exactly how the reference to the class instance will be called in the workspace (in this case, "test"). You can work with it by passing the name in the constructor, or you can use static variables and methods, but this method will not allow you to dynamically change the input signal.
Golmar
source share