MATLAB: I receive this warning : Warning: Variable ‘needed’ exists in base workspace or data dictionary but not in the model’s source workspace.

dstworkspaceerrorgenetic algorithmMATLABsimulinksrcworkspaceworkspace

I want to run an optimization problem using GA, the optimization will be runned for multiple cases of study using For statement.
An objective function (myfitness) is written for the fitness functions and this function will run a Simulink model using a command line as follow
options=simset('SrcWorkspace','current''DstWorkspace','current');
sim('untitled2.slx',[],options)
The whole program is runned by a Main script in which another function 'function 2' is called to define some data and variables to be used in the fitness function (myfitness).
When the main script is runned, i got this warning and the simulation stops
' Warning: Variable 'needed' exists in base workspace or data dictionary but not in the model's source workspace. '
The variable 'needed' is obtained by calling the 'function 2' , i understand that Simulink don’t have the access to this variable , but i can see that this variable is on the workspace.
What is the solution please and what is the cause of the problem.
Best

Best Answer

The solution is to use the output variables from the function 2 as global, and the problem is solved