Workspace definition of "a variable" doesn't match this m-scripts definition

1 view (last 30 days)
I got an error message: Error executing "PreSimFcn" on Simulation input object. Workspace definition of "EngineeringModel" doesn't match this m-scripts definition.
I set up a for-loop to run simulations with a Simulink/State flow model. It completed loop 1 simulation successfully. When moving to loop 2, I got this error.
For example, in the entry point of the m-script file. it looks like:
clear;
close all;
clc;
cycles = [1 2 3 4 5 6];
numOfCycles = numel(cycles);
numOfWorkers = 3;
numOfBatch = ceil(numOfCycles/numOfWorkers);
for ii =1:numOfBatch
startII = (ii-1)*numOfWorkers+1;
endII = ii*numOfWorkers;
endII = min(endII,numOfCycles);
batchCycles = cycles(startII):cycles(endII);
myTest = myconfig_batch(batchCycles);
%% Select control option
myTest.Runner.NumWorkers = numOfWorkers;
myTest.runAnalysis; % (1) opens simulation manager and run the batch simulations with the Simulink model
clearvars -except cycles numOfCycles numOfWorkers numOfBatch ii
close all;
clc;
end
The first three cycles are completed succefully; but the second three cycles failed and showed the error message above.
I added clearvars; close all in the for-loop and I hope they can clean up the workspace before new batch is launched.
Below is my observation. If I set 3 cycles instead of 6, I can run and complete the three cycles. Then I changed the cycles to {4 5 6] and restarted the script without closing the Matlab, it still completed the three cycles {4 5 6] successfully.
It looks like Matlab doesn't clean up its workspace in for-loop as I expected. There are still something in the workspace that made "EngineeringModel" doesn't match for loop 2.
My question is: how to clean up everything in the workspace except the few variables by using Matlab script as if the m-script restarts?

Accepted Answer

Yuvraj Singh
Yuvraj Singh on 12 Jan 2023
Hi Rui,
I understand you are using a loop to simulate some Simulink/Stateflow model, and you encounter error in the second iteration of the loop.
I noticed error states "EngineeringModel" definition doesn’t match the m-scripts definitions. “EngineeringModel" definition is not a variable.
‘clearvars’ removes all variables from currently active workspace. It doesn’t clear any functions, classes or MEX functions.
Depending upon your case you might have to clear functions, classes or MEX functions. ‘clear’ provides options for the same. Please refer to the documentation mentioned below for more details.
I hope it helps you.
-Yuvraj
  1 Comment
Rui Zhang
Rui Zhang on 13 Jan 2023
Moved: Steven Lord on 13 Jan 2023
Hello Yuvraj,
Thank you for your reply.
I found my code to clear variables in the base workspace. "EngineeringModel" is not in the base workspace. It is in the worker workspace in the parallel runs. That is why it doesn't work. I need to clear each of the worker workspaces in the parallel runs before loading another task into the same worker.
I don't know if you knew how to clear the Simulink object "EngineeringModel" in the worker workspace?
Thank you.
Rui

Sign in to comment.

More Answers (0)

Categories

Find more on Modeling in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!