1
votes

I have a question with regards to the Matlab codes that simulate Simulink models. For instance my system right now comprises a few different subsystems/codes:

  1. The main script – sets up initial conditions for the simulated Simulink model and also saves and plots some of the outputs (Main code).
  2. Simulink model that mimics operation of my system.
  3. Sub script that is a function block in the Simulink model. In this case this block derives some of the values that are used in the model during simulation (Sub code).

Right now I am trying to introduce an additional block into one of my subsystems in Simulink. Just to simplify it is a constant block with the variable named “V”. I am assigning an initial value of V in the sub code. But when I run the Main code that will execute the model (with a constant block V inside), it gives me the error than V is not defined. If I initialise V in the main code it will have the declared value, regardless of any manipulations with V in the Sub code.

Hopefully I described the issue more or less well, because I can’t publish the model here. Thank you in advance.

1
Not an good solution, so I will not make it an answer, but have you tried declaring it a global variable? I am not sure about the scopes of Simulink variables, I can just remember, that pushing variables to and from the main workspace was a pain.crown42
Hi, I've double checked now. Declared as a global variable with a Data Memory store block in Simulink, but the outcome is the same. As soon as I delete the variable from the Main Code, the model won't be compiled.Efim Sturov
A simulink model requires all data to be available during its initialization. Nominally you need the "sub-script" (which is a very bad name for it) to be initialized (and hence create the variable V in the Workspace) before the constant block using V gets initialized. However, more fundamentally, you shouldn't be using the Workspace to do this at all, and you certainly shouldn't be initializing a one block based on code being executed in another block. But without knowing exactly what you're really trying to achieve with this configuration it's not possible to tell you how it should be donePhil Goddard
The Sub script is called MechAccDerivation in real life. Does it give any useful information? Don’t think so. That’s why it is common sense not to use real names on this website, but provide smth that is shorter and descriptive at the same time. The main point is that when V is initialised via the Main Script, it does not change its value, even though it should be over written by the sub script.Efim Sturov
It is still unclear, what exactly your setup is. I can think of several ways how one could implement what you are describing. Is the main script a regular matlab script? How is the sub script called, I understand that its a fcn block? Are you using callback functions? These are things that determine the scope of the variable and what Phil Goddard alluded to. It is what we need to know, to properly help you.crown42

1 Answers

0
votes

Ok, after your clarification, let me take a stab at an answer here.

First of all, to initialize your data: Back when I was taking classes on Simulink, they told us initialize via Callbacks. Specifically you have a block, possibly highlighted in some way, where you access Callsbacks via rightclick. There you find InitFcn, and either type some constants there, or the name of a script in the same folder. Then, when you run your model, it is initialized, the callback function is called, and your variables are defined within the Model workspace.

By the way, you can check your model (and function) workspaces in the model explorer, accessed via a button next to library.

Now for your update via function. I am still not certain how exactly you do it. Unless you are already doing it, I suggest using a Matlab function block, the one with the Matlab icon, which is more flexible and powerful.

Anyhow I suspect the following. As with normal matlab, variables defined or changed in functions do not leave its workspace, which is separate from the general model workspace. Only explicit outputs survive the end of the function call.

I see a few options here. The first would be to define the variable as global. For this you need to define it as global in every context where you want it. So you need a global x both in your main script and in your function (sub_script).

Alternatively, if you want to treat the changing variable as a signal, you could add an additional in- and output to your function and your data store blocks, or From/GoTo blocks, depending on what exactly you do.