Delete an array element using matlab function block in simulink

5 views (last 30 days)
Hi i tried to build this simple code, to delete an element from an array 'u' in input using a matlab function block. This is the code of the function block:
-function y = fcn(u);
-u(u==2) = [];
-y = u;
I can not understand why i obtain this error : Dimension 1 is fixed on the left-hand side but varies on the right ([3 x 1] ~= [:? x 1]). I tried to set the variable size from 'Edit data' in the function editor but doesn't work. Someone can help me please?
  2 Comments
Junyan Du
Junyan Du on 19 Feb 2020
i met this problem too, just cant figure it out why this doesnt wrok in Simulink block. lol

Sign in to comment.

Answers (1)

Rajanya
Rajanya on 10 Feb 2025
The reason for this error is that the code in the MATLAB Function Block is trying to change the size of the input itself. 'Variable size' property applies only to variables with 'Scope' property set to 'Output' - Define MATLAB Function Block Variables.
Modifying the code like the following resolves the error since it creates a copy of the input first and then removes one element from there-
function y = fcn(u)
y=u;
y(u==2)=[]; %delete the element from 'y' and not 'u'
Also, the output variable 'y' should be set to 'variable size' and the size can be specified to be 'size(u)' to prevent the output signal from getting unbounded (otherwise, the display block would not be able to display the output).
Hope this would help. Cheers!

Categories

Find more on Simulink Environment Customization 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!