Code generation with external variables
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Alexander Babin
el 25 de En. de 2021
Comentada: Alexander Babin
el 25 de En. de 2021
Dear friends!
I am not sure if the word 'external' in the question is correct, but here is what i am talking about:
The program takes a 0 or a 1 at each ime step and outputs a 0 or 1 depending on some simple logic. What I want is to define some variable before this logic starts, and then update the variables withing the logic. The code in C/C++ would look like this:
void main(void)
{
int c1 = 0; // a '1' counter
int c0 = 0; // a '0' counter
int in = 0; // current bit
while(1)
{
in = ReadBit(); // function reads a bit (in simulink i just have a .mat file with all bits read already)
if (in == 1)
c1++;
else
c0++;
if (c1 == 2*c0)
{
doSomething();
// reset the counters
c1 = 0;
c0 = 0;
}
}
}
So, i want to have a subsystem in a Simulink model, that reads initial values of some parameters, then it will update some counters and flags and then, as it gets another bit, it could use the 'previous' values of the counters and flags.
I belive it can be somehow done with a Data Store Memory block, but I've never used it, so I am not sure.
Thanks a lot in advance!
5 comentarios
Birdman
el 25 de En. de 2021
As far as I understand from your problem, if you want to be able to write to a specific variable at one time step and keep it the same until the next writing, either MATLAB Function approach our usage of Enabled Subsystem approach should work. If you will go on with MATLAB Function, then I suggest you to use persistent variable. Take a look:
They are compliant with code generation.
Respuesta aceptada
Walter Roberson
el 25 de En. de 2021
You can translate that pretty directly into a MATLAB function block that uses some persistent variables.
function triggered = fcn(in)
persistent in, c0, c1;
if isempty(in)
in = 0; c0 = 0; c1 = 0;
end
triggered = 0;
if in == 1
c1 = c1 + 1;
else
c0 = c0 + 1;
end
if c1 == 2*c0
triggered = 1;
c0 = 0;
c1 = 0;
end
end
The output of this would be a signal that would be 1 if you need to doSomething, so you would push the output through an if block or use a triggered subsystem.
Más respuestas (0)
Ver también
Categorías
Más información sobre Naming Conventions en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!