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.
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
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.