Can MATLAB function blocks be set to run once per simulation instead of each time step if the outputs are used as inputs to a 1D look-up table?

5 vues (au cours des 30 derniers jours)
I have a custom MATLAB function block that takes several constant inputs and outputs a setpoint that is then used as an input to the main model block via a 1D look-up table. Currently this custom function block is running at each time step, which is very commutationally expensive.
I have successsfully used a previous answer, linked below, to get this block to run only once per simulation. This was my original intention but it then causes an error when the setpoint is passed to the look-up table block, see error below.
Error = "Output argument 'x' is not assigned on some execution paths"
Is it possible to define this custom function block to only run once per simulation then use the output as an input for each time step without re-running the custom function block?
I've attempted to create a concept sketch below as I am unable to share more details of the working model.

Réponse acceptée

Paul le 16 Jan 2023
Modifié(e) : Paul le 16 Jan 2023
That link includes three different solutions, not sure which one you're using. Having said that, not sure how the error message is relevant, especially because it refers to an output variable 'x' that is not an output from fcn.
If you want the code in fcn to execute once at simulation initialization, it might be better to use a model callback function or a block callback function.
I wasn't able to figure out how to control the sample time of the Matlab Function block so that it only executes once; thought that would be easy.
If the Matlab function block is needed, can always do something like this
function [Output_Breakpoint,Ouput_Table] = fcn(C1,C2)
persistent hasrun localbreak localtable
if isempty(hasrun)
hasrun = 1;
localbreak = % compute breakpoints based on C1, C2
localtable = % compute table data based on C1, C2
Output_Breakpoint = localbreak;
Output_Table = localtable;
This approach saves the cost of the computations at the potential expense of the copy. However, I'm not sure if the generated code would actually do a data copy every step, or if it would be optimized away.
Or, keep the Matlab Function block you have, select it, open the Property Inspector, set the Update Method to Discrete and the Sample Time to a value larger than the maximum simulation time (assuming you know it) like 1e10 or something like that.
  1 commentaire
Jack Smith
Jack Smith le 17 Jan 2023
Hi Paul,
Sorry, forgot to mention I tried the clock and mod function block method shown in the above link. Also, 'x' was one of the outputs of my MATLAB function block, I changed the names in the image included on the original question to make it clear that the output connected to a look-up table block.
Changing the update method of the fcn block to discrete and increasing the sample time to a value larger than the simulation time worked well. Thanks for the answer!

Connectez-vous pour commenter.

Plus de réponses (0)


En savoir plus sur General Applications dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by