Just the other day I was running a program in Simulink and I found a problem with global variables. The solution finally was really easy but I wanted to share it with you.
First of all, in order to initializate global variables, we can go to my previous post 'Clock Pulses Counter' where I explained how to do it in detail.
Back to the problem, I wanted to create an array of global variables in a Matlab function block. I realized that in order to do it, I just had to initializate the Data Store Memory into an array:
Array initialization Data Store Memory |
Now, this global variable behaviours as an array.
Other thing I had to face was write and read a global variable out of a Matlab function block (in order to debug the code). Then I found some really interesting blocks: Data Store Read and Data Store Write, that allow us to read and write into the global variable.
Data Store Read |
Data Store Write |
With that, I wanted to implement an example with both combinations: write and read global variables in a Matlab function block and write and read the same global variables with the Data Store Read and Data Store Write blocks.
The example I thought was to generate a 3-array and the values will be incremented by 1 or by 5 depending on the block reading and writing the global variables. The result was the following:
Example: General Block Diagram |
Then, inside the 'Increment counter with Matlab function block' we can find the following blocks:
Matlab function block |
By including the Trigger block or Enable block inside a subsystem we make the subsystem triggered or enabled by the outside. The code inside the block was:
function fcn
global counter_array;
global index;
if index < 3
counter_array(index) = counter_array(index) + 5; %add 5 to the value
index = index +1;
else
counter_array(index) = counter_array(index) + 5; %last index value
index = 1;
end
Inside the 'Increment counter reading and writing global variable' we can found the following blocks:
Increment counter reading and writing global variable block |
Now, inside the 'Index < 3' block:
Index < 3 block |
Here, depending on if the index is 1 or 2 we will develop different performances.
In the case of Index ==1:
Enable Add1 |
The value inside array(1) will be increased by 1 and also the index:
Index++ |
In the case of index different from 1, we want to keep the array(1) and array(3) values, since we are changing index = 2, so the diagram block will be:
Enable Add~1 |
In the case of 'Enable Add 2' and 'Enable Add~2' is the same as the blocks before but changing array(1) to array(2).
Nevertheless, in the case Index = 3, the block diagram will be the following:
Index = 3 |
And inside index = 1 block is just as simple as write a constant = 1 inside the Data Store Variable.
Index = 1 |
Finally, the theoretical result of that example should be:
Index = 1 Array[3] = [0,0,0]
Index = 2 Array[3] = [5,0,0]
Index = 3 Array[3] = [5,1,0]
Index = 1 Array[3] = [5,1,5]
Index = 2 Array[3] = [6,1,5]
Index = 3 Array[3] = [6,6,5]
Index = 1 Array[3] = [6,6,6]
Index = 2 Array[3] = [11,6,6]
Index = 3 Array[3] = [11,7,6]
...
And the simulation we got was the following:
Example: Final Simulation |
It can be seen that it is as expected.
Notice that is much more complicated read/write global variables with the 'Data Store Read' and 'Data Store Write' blocks than with the 'Matlab function' block. However, they are very useful in order to debug our code.
Hope this is useful and any comments with problems or recommendations are welcome!! See you soon!