Input ("u") index must be a positive integer in the expression: 1.5*u[n]-0.5*u[n-1]

2 views (last 30 days)
I am running MATLAB R2013a and I am attempting to convert a project into C++ code. When I have my model opened in Simulink and hit Ctrl+B, I get the following error:
Input ("u") index must be a positive integer in the expression: 1.5*u[n]-0.5*u[n-1] in 'fcn' when used for code generation
I am not sure how to move past this error. I do not have much MATLAB experience so I am not sure what other information to include in this post. I saw in another post that a method for fixing this involves replacing the function with a switch statement, but I have not been able to figure out that, either.
Here is what the code to my function block in my mdl looks like:
Block {
BlockType Fcn
Name "Fcn"
Position [685, 339, 755, 371]
BackgroundColor "yellow"
Expr "1.5*u[n]-0.5*u[n-1]"
}
And I believe this chunk of code from an m-file might be related:
h_1a = Hmg(Sc_1a*Sf,th(1),Np(1),Tm1a,Td,1,Ta,Pa);
Out_1a = 1.5*h_1a(2*n)-0.5*h_1a(2*n-1);
h_1b = Hmg(Sc_1b*Sf,th(2),Np(2),Tm1b,Td,1,Ta,Pa);
Out_1b = 1.5*h_1b(2*n)-0.5*h_1b(2*n-1);
Out_1 = ((Np(1)*(H(1)-th(1))*(s(1)-th(1)))*Out_1a+(Np(2)*(H(1)-th(2))*(s(1)-th(2)))*Out_1b)/(Np(1)*(H(1)-th(1))*(s(1)-th(1))+Np(2)*(H(1)-th(2))*(s(1)-th(2)));
h_2 = Hmg(Sc*Sf,th(3),Np(3),Tm2,Out_1,2,Ta,Pa);
Out_2 = 1.5*h_2(2*n)-0.5*h_2(2*n-1);
h_3 = Hmg(Sc*Sf,th(4),Np(4),Tm3,Out_2,3,Ta,Pa);
Out_3 = 1.5*h_3(2*n)-0.5*h_3(2*n-1);
h_4 = Hmg(Sc*Sf,th(5),Np(5),Tm4,Out_3,4,Ta,Pa);
Out_4 = 1.5*h_4(2*n)-0.5*h_4(2*n-1);
h_5 = Hmg(Sc*Sf,th(6),Np(6),Tm5,Out_4,5,Ta,Pa);
Out_5 = 1.5*h_5(2*n)-0.5*h_5(2*n-1);
h_6 = Hmg(Sc*Sf,th(7),Np(7),Tm6,Out_5,6,Ta,Pa);
Out_6 = 1.5*h_6(2*n)-0.5*h_6(2*n-1);
I can provide more information about the project on request.

Accepted Answer

Zack Peters
Zack Peters on 29 Oct 2013
Hi Alexa,
I was able to reproduce the issue on a much smaller scale (see attached). The BlockType of Fcn above makes me believe that it is point to a Fcn block. I cannot confirm this without seeing your model, but is there any chance that you have a Fcn block named "Fcn" that has an expression of "1.5*u[n]-0.5*u[n-1]" in your model?
If so then that block is the culprit, and in general you wont be able to add indexing for the Fcn block. If no such block exists, then this may be a trickier issue to track down. Perhaps it is the code you listed above (is that code from a MATLAB Function block?). If so then you may want to try using an assert command in your MATLAB code so that the code generation process knows that n will not be a negative index.
~Zack
  2 Comments
Alexa
Alexa on 29 Oct 2013
Thanks for the answer, Zack. I do have a Fcn block with that expression. Does this mean that I will be unable to generate C++ code from that model?
The first block of code from above is in a .mdl, but the second is from a .m file. I put in assertions with the .m code, but it still will not generate code.
Zack Peters
Zack Peters on 29 Oct 2013
What is the expression in the dialog for the Fcn block? And what are you trying to implement with that block?
"1.5*u[n]-0.5*u[n-1]" is not an acceptable expression. It seems like you may be trying to use a discrete state where u[n] is the current input and u[n-1] is the input from the previous time step? If this is the case then we can cannot model this with the Fcn block as it does not contain any discrete states, but you can definitely model it with other blocks such as the Discrete Transfer Fcn block.
~Zack

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!