calling fi(var1, var2,..varN) in matlab function in a simulink model asks for constant values for input 2 to N
5 views (last 30 days)
I want to cast a value into fixed point in a matlab function in a simulink model. To do so I have a SLDD used by the model in which some parameters are defined for word length with the sotrage calss as const (I had the same issue with auto)
I made a function with several inputs (this is an example that gets the same error as my function) :
function [out1] = horCounter(in1, in2, W_HDISP)
out1 = in1 + fi(in2,0,W_HDISP,0);
I defined W_HDISP as a parameter in the symbol pane:
Whenever I try to simulate I get the following error message:
Inputs var2..varN in call to fi(var1, var2,..varN) must be constant. Input 3 is not a constant.
Function 'GENERIC/PATH/TO/FUNCTION/MATLAB Function' (#111.111.111), line 2, column 17:
Launch diagnostic report.
Is there a way to solve it in the function or do I have to put conversion blocs on my in2 before sending it in the function and get rid of the fi() function use?
Andy Bartlett on 16 Jun 2023
I don't know if there is or is not a way to make the MATLAB Function block recognize that W_HDISP as a constant at update diagram time.
But there is a very powerful approach to achieve your goal. The approach is to follow the Cast Like pattern.
prototypeVarHDISP = fi(,0,16,0); % put in SLDD or where desired
function out1 = horCounter(in1, in2, prototypeVarHDISP)
out1 = in1 + cast( in2, 'like', prototypeVarHDISP);
In the Cast Like pattern, you create a "dummy" numeric variable whose purpose is just to communicate data type information. The value held by this variable doesn't matter; that's why I'm calling it a "dummy" variable. Only the numerictype of the "dummy" variable matters. This "dummy" variable is more elegantly called a Prototype.
The Prototype can be an individual argument to the function as in the example code above. Or, if you need multiple prototypes, then you can organize them into a structure as shown in this example in the documentation Write MATLAB Code That Is Independent of Data Types.