Dang Khoa
Dang Khoa

Reputation: 5823

Simulink - programmatically specifying external inputs

I would like to adjust some Simulink parameters programmatically, namely specifying external inputs and initial state. The Mathworks' help document on Data Import/Export shows how to do so both visually and programmatically. Apparently for external inputs, I need to specify LoadExternalInputs to 'on'; I would like to override the default external input via ExternalInput to [myTimeVar, myInputVar], as documented in that link.

Trying to run the command

sim('mySim', 'LoadExternalInputs', 'on', 'ExternalInput', [myTimeVar, myInputVar]);

yields the following error message:

Invalid setting in block_diagram 'mySim' for parameter 'ExternalInput'

If I'm not supposed to specify my external input in the sim() call, where am I supposed to do it? I'm quite confused as to how to specify my inputs and states.

Upvotes: 2

Views: 4032

Answers (3)

josemrivera
josemrivera

Reputation: 113

As pointed out by @MikeT, the sim command works in a parameter name-value fashion, so both the parameter name and value must be strings. The command should be:

sim('mySim','LoadExternalInput', 'on', 'ExternalInput', [myTimeVar, myInputVar]);

I believe the parameter 'LoadExternalInput' is not necessary, but in any case, it is 'LoadExternalInput' rather than 'LoadExternalInputs'. I checked it with get_param('modelName','ObjectParameters')

Some important remarks:

Remark #1:

When using sim, Simulink automatically switches the workspace to the model's, so one might get errors if sim is executed within a MATLAB function or unitTest.

Remark #2

In the pair 'ExternalInput','Inputs', 'Inputs' should contain the column-wise concatenation of all the column vectors inputs, starting with the time vector: '[myTimeVar, myInputVar1, myInputVar2, ..., myInputVarN]', all of them column vectors.

Upvotes: 2

MikeT
MikeT

Reputation: 1644

The ExternalInput parameter must be a string, try this:

sim('mySim', 'LoadExternalInputs', 'on', 'ExternalInput', '[myTimeVar, myInputVar]');

Upvotes: 5

Kevin K
Kevin K

Reputation: 9584

help sim in MATLAB shows me a different command format than what you're using:

[t,x,y] = sim(model,timespan,options,ut);

I think maybe this is what you want?

sim('mySim', myTimeSpan, [], [myTimeVar, myInputVar]);

(where myTimeSpan is the time span to simulate over)

Upvotes: 0

Related Questions