필터 지우기
필터 지우기

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

조회 수: 6 (최근 30일)
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?

채택된 답변

Yuvraj Singh
Yuvraj Singh 2023년 1월 12일
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
Rui Zhang
Rui Zhang 2023년 1월 13일
이동: Steven Lord 2023년 1월 13일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Modeling에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by