How to set Matlab function block output as fixed (non-tunable) variable

2 views (last 30 days)
I have been trying to clean some dust of my Simulink models shelves and encounter the following hurdle:
I'm using a Matlab function simulink block to output 2 scalar numbers vectIndx and tableNum. While trying to run the simulation (which run perfectly well with Matlab 2016B), I'm getting the follwing error:
'vectIndx' is inferred as a variable-size matrix, but its size is specified as inherited or fixed. Verify 'vectIndx' is defined in terms of non-tunable parameters, or select the 'Variable Size' check box and specify the upper bounds in the Size box.
Component:MATLAB Function | Category:Coder error
I have tried to add
vectIndx=zeros(1)
at the top of my funciton code but that didn't work either.
I have run the same block while disabling the function output variables via dubg mode and made sure these outputs holds scalar numbers.
I have read the following post: https://www.mathworks.com/matlabcentral/answers/513980-why-are-my-matlab-function-block-arguments-being-inferred-as-variable-size but didn't quite get how to set the outputs as non-tunable.
Please see my code bellow:
function [vectIndx,tableNum] = fcn(mabc,Uabc,delt,vDrop,vectNum)
vectIndx = zeros(1,1);
tableNum = zeros(1,1);
%#codegen
Uabc = [Uabc; 0]; % last entries for zero voltage drop
delt = delt';
i = find(vectNum == [100 10 1]*mabc); % current vector representation
tableNum = i(1);
regMtrx = -1*Uabc(abs(vDrop(:,:,i))).*sign(vDrop(:,:,i)).*repmat(delt,size(vDrop,1),1);
[~, vectIndx] = max(sum(sign(regMtrx),2));
Cheers!
ML

Accepted Answer

Manny Litz
Manny Litz on 12 Feb 2022
Hello all,
It seems some of the matlab funcitons I have used return variable matrix by default. This leads matlab to asume potentially variable matrix output and hence to rais the above error. My workaround was to declare another temporary variable as follows:
vectIndx = 0;
vectIndxTemp = 0;
I then used the temprorary variable to hold the result and used indexing method to pass it on to my output variable like so:
[~, vectIndxTemp] = max(sum(sign(regMtrx),2));
vectIndx = vectIndxTemp(1);
Problem solved.
Cheers, ML

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!