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

조회 수: 7 (최근 30일)
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 ?

답변 (1개)

Walter Roberson
Walter Roberson 2018년 1월 20일
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
Walter Roberson
Walter Roberson 2018년 1월 20일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Interactive Model Editing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by