simulink error Input ("u") index must be a positive integer in the expression when used for code generation

I am running MATLAB R2015a and I am attempting to covert a project. When I have my model opened in Simulink and simulate, I get the following error: simulink error Input ("u") index must be a positive integer in the expression when used for code generation
Please, how can I solve the problem?

2 Comments

Would you happen to be using logical indexing in a MATLAB Function block?
hello Roberson, no logical indexing, but numerial indexing. I want to simulate my model in Simulink external mode. In my model I have used a FCN-block. Please, do you have an idea how I could solve the problem?. thank you in advance

Sign in to comment.

Answers (1)

"x = strmatch(str, strarray) looks through the rows of the character array or cell array of strings strarray to find strings that begin with the text contained in str, and returns the matching row indices. If strmatch does not find str in strarray, x is an empty matrix ([])."
Your code does not take into account the possibility that there are no matches.
Also as far as MATLAB can tell at that point, you could be returning multiple values because MATLAB has no way of knowing that there will be only one match.
You will need to use a MATLAB Function block, and in the block you should use strncmp() to build a logical vector of matches. You can then check for the case of no match with ~any() and handle that case, and in the else, you can find(mask, 1, 'first') so that you can be sure you got exactly one result.
Code Generation does not have the flexibility that MATLAB has: it needs to be guided about all these possibilities about no matches, multiple matches, and so on. Even in the case where logically you "know" that you should have exactly one result, you need to fill in the rest for Code Generation because Code Generation wants to be certain that there will be no undefined results in the section it is generating (which might, after all, get reused.)

6 Comments

Dear Roberson, Thanks for your answer. I'm using your answer and try to solve my problem
Dear Roberson, can you help me please? I can not do with the Matlab function block to read the exact value. I have 40 input values. How can I do that I get the exact values? Is there the ability to read multiple values simultaneously? I'm new in Matlab thank you.
Which values are you trying to read?
What are your inputs? What are your outputs?
One thing to remember is that Simulink does not have a "string" or "char" signal type, so if you want to pass strings around you either need to use a custom bus or you need to pass a numeric array and char() the array inside your function.
I try to read values from the output of a S-Funktion. The Output of the S-Funktion ist a OutDate and this Data have a List of 40 Values. For Now I have 4 Inputs. e.g. I want to take one output value to input value of my genertor Torque and Power. Or 2 Values from outdata for Yaw or Pitch Controler later. This S-Funktion ist from Nrel FAST
Dear Walter, please, do you know how can I read more Outputs values simultany using MATLAB function block (Code Genaration).

Sign in to comment.

Categories

Asked:

on 11 May 2016

Commented:

on 15 Jun 2016

Community Treasure Hunt

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

Start Hunting!