Well I don't know how to do it from a simple function, but it's really handy to do it from inside a class function (method). It works fine with version 2009b.
Place the code in file Test.m:
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 happens? You create object test, that has defined fields t and u. Then in method sim() You say to Simulink to look for input '[test.t test.u]'. Both Simulink and the method sim() has access to this variables (I believe this is the most important thing).
OK it still has a big drawback that is marked with number 1. You have to know explicitly how a reference to class instance will be named in the workspace ('test' in this case). You can work it around by passing the name in a constructor, or you can use static variables and methods, but this way won't allow you to change input signal dynamically.