- Preallocate Arrays with Maximum Size: Instead of dynamically creating arrays with varying sizes, preallocate arrays with a fixed maximum size and then fill only the required elements.
- Use Conditional Logic for Indexing: and fill relevant parts of preallocated arrays based on runtime values.
Why am I getting a computed maximum size is not bounded error in a MATLAB Function block in Simulink R2021a even when using assert?
21 次查看(过去 30 天)
显示 更早的评论
I am using a Matlab function in Simulink and want to create arrays whose size varies. However, I can specify the maximum size in advance. I got the following error message, although I am already using the assert function.
Computed maximum size of the output of function 'colon' is not bounded. Static memory allocation requires all sizes to be bounded. The computed size is [1 x :?]. More informationFunction 'Control_Model/Current Controller ILR and PI/MATLAB Function' (#24.2546.2574), line 86, column 24: "iL_NL_act:1:(iL_NL_act_plLL)" Launch diagnostic report.
Component: MATLAB Function | Category: Coder error
Computed maximum size of the output of function 'colon' is not bounded. Static memory allocation requires all sizes to be bounded. The computed size is [1 x :?]. More informationFunction 'Control_Model/Current Controller ILR and PI/MATLAB Function' (#24.2602.2630), line 87, column 24: "iR_NL_act:1:(iR_NL_act_plLL)" Launch diagnostic report.
Here is the corresponding code:
LL = 7;
assert (varyingInput_a <= 800)
assert (varyingInput_b <= 800)
varyingInput_a_plusLL = varyingInput_a+LL;
varyingInput_b_plusLL = varyingInput_b+LL;
assert (varyingInput_a_plusLL <= 800)
assert (varyingInput_b_plusLL <= 800)
iLrange_act(1:LL+1) = (varyingInput_a:1:(varyingInput_a_plusLL))-m;
iRrange_act(1:LL+1) = (varyingInput_b:1:(varyingInput_b_plusLL))-m;
I had the same problem before in the same Matlab function for an input variable from which the variables used here are calculated. There I also used the assert function and the error no longer occurred. I would have expected that using the function once before using the variable for the first time would be enough to limit all other variables derived/calculated from it.
Here is the code from the first assert use:
assert (N_act <= 800)
Angel4Nact = [1:1:N_act]*(2*pi/N_act);
varyingInput_a and varyingInput_b are calculated using N_act
0 个评论
采纳的回答
akshatsood
2024-8-19,14:36
编辑:akshatsood
2024-8-19,14:42
I understand that you are encountering an error message when using variable-size arrays in your model.
The error message indicates that MATLAB is unable to determine a fixed maximum size for the arrays generated by the colon operator (:) in "MATLAB Function" block. This is because the (:) operator generates arrays whose size depends on runtime values, and Simulink requires that all array sizes be bounded at compile time for static memory allocation.
Even though you are using "assert" statements to constrain the maximum values of "varyingInput_a" and "varyingInput_b", the colon operator in lines like "(varyingInput_a:1:(varyingInput_a_plusLL))" can still produce arrays with sizes that are not explicitly bounded at compile time. The issue here is that the size of the array depends directly on "varyingInput_a" and "varyingInput_a_plusLL", which can differ.
To resolve this, the following steps should help:
LL = 7;
maxSize = 800 + LL;
% preallocate arrays with maximum size
iLrange_act = zeros(1, maxSize);
iRrange_act = zeros(1, maxSize);
% assert - varyingInput_a and varyingInput_b do not exceed their bounds
assert(varyingInput_a <= 800);
assert(varyingInput_b <= 800);
varyingInput_a_plusLL = varyingInput_a + LL;
varyingInput_b_plusLL = varyingInput_b + LL;
for idx = 0:LL
iLrange_act(idx + 1) = varyingInput_a + idx - m;
iRrange_act(idx + 1) = varyingInput_b + idx - m;
end
% trim the arrays to the actual size needed
iLrange_act = iLrange_act(1:LL+1);
iRrange_act = iRrange_act(1:LL+1);
With these changes, you should be able to avoid the error related to unbounded array sizes in your model.
I hope this helps.
更多回答(1 个)
Fangjun Jiang
2024-8-19,14:37
The Simulink diagnostic is probably not designed to read the assert() function.
The proper way for this is to define those data as variable-size data (For example, iLrange_act, iRrange_act, etc. Those data that are matrix, not those size parameters such as varyingInput_a_plusLL, varyingInput_b_plusLL) and define its max size.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!