Migrating from Matlab 2015b to 2017b error

23 views (last 30 days)
I have a model that can be run in Matlab 2015b & 2013b, but when I try to run in 2017b, I get the following error.
MATLAB Function uses constructs that are invalid when the block specifies or inherits a continuous sample time. Invalid constructs include the use of persistent or global variables, calls to exported functions or using coder.extrinstic. Suggested Actions Set sample rate of this block to inherit the fastest discrete rate in the model. Fix Open the properties dialog of this block to set the sample time manually.
  8 Comments
Thomas Kemmler
Thomas Kemmler on 20 Feb 2019
I could not solve the error, but I created a model that shows the problem.
Attached is a simple model for Matlab 2018a and one for Matlab 2017a. When you open the 2017a model (Smallmodel2017a.slx) and run it, no error occur and all the output variables have the same size (i.e. 52070x1 double).
When you run the same model in 2018a (Smallmodel.slx), the mentioned error of Sohrab occur and the suggested solution of simulink is to change the update method from inherited to discrete with a sample time of -1. With this fix, the simulation is running, but the output variables are different than in 2017a. The output variable "integrator" is 52552x1 double, but the outputs of the Matlab function only is i.e. 1153x1 double.
The problem now for me is, that I need to identify at every iteration step, whether a 1 or a 0 comes out of the Matlab function and that is not possible with the 2018a model and the suggested fix.
The function itself looks as followed, the input "simulationtime" is the Block "clock" to output the current simulationtime.
function [HP_on_off, numofschedu ] = Schedule(simulationtime)
%% Parameter set
%delta of time for next schedule generation; 24h respective 1440 min.
dt_FPnew = 1440;
%% Persistents
persistent heatpump_on_off;
if isempty(heatpump_on_off) ; heatpump_on_off = zeros(1440+60,1); end
persistent t_schedule_new; %Time for new schedule generation
if isempty(t_schedule_new); t_schedule_new = floor(simulationtime/60); end
persistent lastsched; %Moment, when last schedule was generated
if isempty(lastsched); lastsched = floor(simulationtime/60) - dt_FPnew -1; end
persistent numofsched; % Number of generated schedules
if isempty(numofsched); numofsched = 0; end
%% Simulation time
t_act = floor(simulationtime/60); %Transform simulationtime in minutes
% Create new schedule after 24h (1440 minutes)
if t_act == t_schedule_new
t_schedule_new = t_act+ dt_FPnew;
lastsched = t_act;
numofsched = numofsched +1;
%schedule generation...
schedule = randi(2,[1 1440])-1;
heatpump_on_off(1:1440) = schedule;
heatpump_on_off(1441:end) = ones(60,1)*schedule(end);
end
%Identify state for the current time step
idx = max(round(simulationtime/60-lastsched)+1,1);
HP_on_off = heatpump_on_off(idx);
numofschedu = numofsched;
end
To simulate the "real" model (not the ones attached) with a fixed step solver works, but it takes much longer than with variable step which for my purpose, is not acceptable.
Does someone know a solution for this? Thank you very much in advance.
Daniel Sadono
Daniel Sadono on 7 Jul 2021
Edited: Daniel Sadono on 7 Jul 2021
@Sohrab Safavi @Thomas Kemmler have you tried this solution? Enable continuous-time MATLAB functions to write to initialized persistent variables - MATLAB & Simulink (mathworks.com). It's written that since 2017B, we have to turn it on manually. I'm trying the same thing on matlab 2018B, but I couldn't find where the menu/option is. @Rik @Birdman

Sign in to comment.

Answers (1)

Jakub Jarosinski
Jakub Jarosinski on 28 Jun 2023
I finally found it. Im writing here so others wont have to waste 2 hours on this. While in 'simulation' tab enter 'Model Settings', in the search bar there search for 'persistent', it will show you the setting. (Matlab 2022)

Community Treasure Hunt

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

Start Hunting!