0
votes

I have a piece of code that works perfectly when run from the command line, but when it is run inside of a Matlab function block in my Simulink simulation, I get a matrix dimensioning error.

Using the debugger, I have figured out that the for-loop is behaving differently from how I expect and I'm not sure how to fix it without major re-writes to someone else's code.

Here is the snippet of code that is causing problems.

for k=input
        idx=4+2*(i-1)+1;
        yhat=yhat+th(idx)*cos(k.*(2.*pi.*60.*t+th(1).*t))+th(idx+1)*sin(k*(2*pi*60*t+th(1)*t));
end

Normally, k would take on each value from 2 to 13 and this would run as one would expect a for loop to run. For some reason, when I set a breakpoint on the yhat line, I am finding that k is the vector [2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13], which is the transpose of what I expect input to be. Does anyone know what is going on that would make this code run differently in the Matlab Function block or how to fix it?

1
Where do you define the input variable?Juan Carlos Ramirez

1 Answers

0
votes

facepalm, I figured it out right after posting the question. Apparently Simulink Constant blocks will output a column vector even when supplied with a row vector.

For loops iterate over the column of the supplied vector, so

for ii = [1 2; 3 4]
    ii
end

outputs [1;3] [2;4] for example. Because the constant block was supplying my input as a column vector, this led to the for loop iterating over the 1 column. Transposing the vector fixed the issue.