Block Error: Undefined function or variable. The first assignment to a local variable determines its class.

9 Ansichten (letzte 30 Tage)
Hi everyone,
I would like to store the attributes of entities that are currently being processes by a server block. In other words, when an entity enters a server block, its attribute is read and stored in a vector. When an entity exits a server block, it's attribute is read and removed from the aforementioned vector. It must be possible to read the vector from other blocks in the model.
I get the following error when I run my model: "Error in 'untitled/Entity Server' block. See Entry action line 5, column 12. Caused by: Undefined function or variable 'ServerStore'. The first assignment to a local variable determines its class. Component:Simulink | Category:Block error"
I have made a basic model and attached it to this post. I have also added the relevant code below. Any help would be greatly appreciated.
Link to the model: https://drive.google.com/drive/folders/14KkH0H5JcUSwr-qqCHbPg7RUXHimafRw?usp=sharing
Entity Generator -> Event Actions -> Generate:
persistent ENTITYNR;
if isempty(ENTITYNR)
ENTITYNR=1;
entity.ENTITYNR=ENTITYNR;
else
ENTITYNR=ENTITYNR+1;
entity.ENTITYNR=ENTITYNR;
end
Server Block -> Event Actions -> Entry
%Record the attribute "ENTITYNR" from each entity that enters the server and store it in NROfNewEntityThatEntered
ENTITYNROfNewEntityThatEntered=entity.ENTITYNR;
%Store the "ENTITYNR" attributes of all the entities that are currently in the server in the vector ServerStore
if isempty(ServerStore)
%If it is the first entity to enter the server
ServerStore=ENTITYNROfNewEntityThatEntered
else
% If there are other entities in the server, add the attribute of the newly arrived entity to the existing ServerStore vector.
ServerStore=[ServerStore; ENTITYNROfNewEntityThatEntered]
end
Server Block -> Event Actions -> Exit
%Read the ENTITYNR attribute of the entity that is exiting the server
ENTITYNROfEntityThatExist=entity.ModuleID;
%Remove the ENTITYNR attribute from the list which is currently in the server
ServerStore = ServerStore(find(ServerStore~=ENTITYNROfEntityThatExist))

Antworten (1)

Krishna Akella
Krishna Akella am 10 Dez. 2018
Bearbeitet: Krishna Akella am 10 Dez. 2018
Hi Striker121,
You are getting the error because the variable 'ServerStore' is not declared in the scope of the event action. Any variables you use in the event action are local to that function and are not visible anywhere outside the event action. So one way I can think of is to use 'assignin' and 'evalin' methods to write to a variable in the base/MATLAB workspace. I have made the following modifications to your model:
Server Block -> Event Actions -> Entry
coder.extrinsic('evalin');
coder.extrinsic('assignin');
assignin('base', 'ENTITYNROfNewEntityThatEntered', entity.ENTITYNR);
%Record the attribute "ENTITYNR" from each entity that enters the server and store it in NROfNewEntityThatEntered
%Store the "ENTITYNR" attributes of all the entities that are currently in the server in the vector ServerStore
% If there are other entities in the server, add the attribute of the newly arrived entity to the existing ServerStore vector.
evalin('base','ServerStore = [ServerStore; ENTITYNROfNewEntityThatEntered];');
Server Block -> Event Actions -> Exit
coder.extrinsic('evalin');
coder.extrinsic('assignin');
%Read the ENTITYNR attribute of the entity that is exiting the server
assignin('base', 'ENTITYNROfEntityThatExist',entity.ENTITYNR);
%Remove the ENTITYNR attribute from the list which is currently in the server
evalin('base','ServerStore = ServerStore(find(ServerStore~=ENTITYNROfEntityThatExist));');
The reason I have added a call to 'coder.extrinsic' is because, during model compile time, all the event actions are first converted into 'C' code, then the C code is compiled in memory and then linked with the model.
I have also declared a variable called 'ServerStore' in the MATLAB workspace:
>> ServerStore = [];
You can also you the model workspace instead.
- Krishna

Kategorien

Mehr zu Discrete-Event Simulation finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by