Matlab code works fine as m file but doesn't work when it's called in a Matlab function block in Simulink

Hi, I'm using a matlab function code (waypoints.m) to generate an array of waypoints. The input arguments are the starting and ending points as two [1x3] arrays. The code works fine when I run it in the editor. But when I create a Simulink Matlab function block, I call the function (waypoints.m) in it, and I input the starting and ending points as two [1x3] arrays in constant blocks, I always get this error repeated in some parts of the code
"Index expression out of bounds. Attempted to access element 3. The valid range is 1-1"
Any help ?

Respuestas (1)

I suspect that the code attempts to grow a vector dynamically after having assigned a scalar to it. In MATLAB Function Block you cannot grow dynamically without taking extra steps. Instead the first assignment to the variable should be be something that is the largest size that the variable will need.
The error could also potentially occur if you tried to reuse a variable, assigning a vector of length 3 after it was originally a scalar.

11 comentarios

"The error could also potentially occur if you tried to reuse a variable, assigning a vector of length 3 after it was originally a scalar."
but this error would also happen in the editor, in which it does not. I just created the function block in Simulink and inserted the function in it, with two constant arrays as input arguments (that's exactly the same as in Matlab editor)..
How can I apply a dynamic vector in a Matlab function block? what are the steps?
The programming language for MATLAB Function Block is not the same as the programming language outside of those. In MATLAB it is completely acceptable to use
A = 5;
A = uint8(8:10) ;
But inside function blocks the data type of a variable cannot change and normally the size cannot increase beyond the size of the first assignment to the variable.
MATLAB Function Block is primarily for use with generating code for systems that do not have dynamic memory allocation, so Simulink wants to know hard maximum sizes so it can allocate memory on stack frames complete with checking that the call will not overflow the memory of the embedded system. Do not think of it as being Simulink just happening to call a MATLAB function: you have to understand it in a code generation context where it needs to worry about stack overflow and buffer overflow.
Hi Walter Thanks for your answer. It helps understanding the issue a bit. Especially when the errors I get include :
- Index expression out of bounds,
- Function call failed, Undefined function or variable 'xxxx'. The first assignment to a local variable determines its class,
- Simulink cannot determine sizes and/or types of the outputs for block 'MATLAB Function' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs....
Any tips how I can still use the function block? another cascaded function block maybe? or anything?
waypoints.m appears to be your own code. If you post it then we can give you some more specific tips on how it needs to be changed.
Hi Walter,
I managed to solve it. Just added
coder.extrinsic
Thank you!
May I ask you, how can I send these waypoints, one by one to a simulink model that tracks the current waypoint and holds up until it's reached , then switch to the next waypoint, and so on, untik it reaches the last one ??
I looked at the github source for that but I do not see any waypoints.m there ?
the waypoints.m is my function. It has no much, just calling the function in github
that's waypoints.m:
function y = waypoints(p0, pf)
%#codegen
y=zeros();
coder.extrinsic('dubins_curve');
r= 50; % The turning raduis
ts= 50; % Step size
y=dubins_curve(p0,pf,r,ts,true);
end
The problem (or at least one of them) is in durbins_core which has
test_param(1,:) = dubins_LSL(alpha, beta, d);
test_param(2,:) = dubins_LSR(alpha, beta, d);
test_param(3,:) = dubins_RSL(alpha, beta, d);
test_param(4,:) = dubins_RSR(alpha, beta, d);
test_param(5,:) = dubins_RLR(alpha, beta, d);
test_param(6,:) = dubins_LRL(alpha, beta, d);
without having initialized test_param first. Because the first assignment determines the size, the size is determined to be 1 row. If you move the last assignment (highest index) to be first then that problem would be bypassed.
Hi Walter, I already bypassed it by adding
coder.extrinsic

Iniciar sesión para comentar.

Categorías

Más información sobre Simulink Coder en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 20 de En. de 2018

Comentada:

el 20 de En. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by