How can I provide input to a Simulink model without placing it in the workspace

I have a Simulink model that is currently executing from a script (i.e. is not a function). The script writes the values ​​of the variables to the MATLAB workspace, starts a model simulation (which uses these values), and then the model writes additional values ​​to the workspace. If I try to convert the script to a function (i.e. by function [output] = runSim() at the top of the file), then Simulink complains that it does not know about the variables, apparently because they are not working MATLAB space, but rather they are in the function area.

Is there an elegant way to provide the Simulink model with input and output the output from the Simulink model, in addition to inserting it into the workspace?

+8
matlab simulink
source share
4 answers

This is not obvious, but you can input / output data from the sim() command and the workspace of the calling function. I have done this before and have an example at work, but cannot get to Monday to check. However, try the solution indicated on the Mathworks website :

Decision:

When using variable mask settings in Simulink, the base workspace is the default workspace for Simulink. However, using the SIMSET command, this workspace can be changed. The SIM card is then used with this option structure created by SIMSET. The following is an example on how to do this.

  options = simset('SrcWorkspace','current'); sim('modelname',[],options) 

... although, apparently, this was deprecated in R2009b due to incompatibility with the Parallel Computing Toolbox> :( It seems that the correct solution is to explicitly push the variables into the workspace of the modeling model (other than the base workspace) using assignin() .

http://www.mathworks.com/matlabcentral/newsreader/view_thread/292544

You have 2 options:

  • For releases prior to R2009b, review the SIMSET documentation. This allows you to set the "SrcWorkspace" property to "current" in order to use the data from your function.

http://www.mathworks.com/support/solutions/en/data/1-1BWDA/?solution=1-1BWDA

  • In newer versions, this option is deprecated because it is not compatible with the Parallel Computing Toolbox and PARFOR. I recommend:

http://www.mathworks.com/support/solutions/en/data/1-ASPEIV/?solution=1-ASPEIV

+9
source share

You can use the evalin () function to execute MATLAB expressions (as strings) from your own function in a specific workspace, in your case "base" for SIMULINK, to find them. However, if you do not want to use the workspace directly, you can load and save signals or variables from / to MAT files using From / To File blocks.

+2
source share

Short answer: None.

I could be wrong , but let me tell you a little. I am working on a Simulink model that is very large, we have been working on this for many years. To this day, we are still loading all the necessary variables into the workspace. It was a complaint for my long time, so much so that MathWorks even addressed this issue by providing the Simulink.save_vars function. It looks like you are already setting up the variables using the script / function, so Simulink.save_vars will not come in handy for you.

You can clear the workspace using structures for some variables, most Simulink blocks do not support structures, but some do. Also, avoid putting anything in the workspace other than the variables that your model needs.

0
source share

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.

0
source share

All Articles