Clear Filters
Clear Filters

calling fi(var1, var2,..varN) in matlab function in a simulink model asks for constant values for input 2 to N

3 views (last 30 days)
Hello,
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);
end
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:
"fi(in2,0, W_HDISP,0)"
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?
  1 Comment
Guy Regev
Guy Regev on 6 Feb 2024
The solution below will not work for HDL code generation. The general solution is to force simulink to regard Input3 i.e. your parameter as constant, and that can be achieved by going to the model tab, click on model explorer. Find this matlab function, click on it and click on the parameter in the parameter list that opened, another window will open on the right hand side (usually) and you'll see the "Tunable" property ticked. tick it off, save and you're done.

Sign in to comment.

Answers (1)

Andy Bartlett
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);
end
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.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!