Variable-Size Input/Output Vectors in Simulink

25 views (last 30 days)
I am using a Simulink Matlab Fcn Function block and keep getting errors related to the variable-size of my input/output vectors. I have one function block that reads in a set of data from a csv file and works fine. The data read is stored in a Data Store Memory Block. The next function block partitions the data into subsets to be processed by the final function block which filters the data twice (using the filtfilt function) and counts the number of peaks in the signal.
My problem is in the function block that partitions the data. Source code below.
function [STOP, subsetAnalyzed] = fcn
%#codegen
global startVal
global voltageArray
global numSubsets
global sizeLastSubset
%coder.varsize('subsetAnalyzed',10000,1);
if numSubsets ~= 0
endVal = startVal + 9999
STOP = 0
else
endVal = startVal + sizeLastSubset
STOP = 1
end
subsetAnalyzed = voltageArray(1, startVal:endVal)
startVal = endVal + 1
numSubsets = numSubsets - 1
In the Ports and Data Manager menu I have checked the Variable Size option, and specified the upper bound of the output subsetAnalyzed to be 10000. Running this yields the following error.
Computed maximum size of output #1 of function 'findpeaks' exceeds maximum allowed number of elements (134217728).
The computed size is [:200000000 x :200000000].
Function 'filterAndCountPeaks' (#41.920.923), line 37, column 2: "pks"
Launch diagnostic report.
Component: MATLAB Function | Category: Coder error
I tried using "coder.varsize('subsetAnalyzed',10000,1);" to specify the upper bound but this did not work either. My understanding is that there must be a value other than '-1' or a blank in the Size section of the Ports and Data Manager menu, otherwise Simulink will expect the value to be scalar.
Below is the third function block that filters and counts the peaks for your reference.
function numPksDisp = fcn(subsetAnalyzed)
%#codegen
global numPks
Z2=1:1:size(subsetAnalyzed,1);
Z2=Z2';
total2=size(Z2,1);
length2=size(subsetAnalyzed,1);
time2=Z2(1:1:length2,1);
%%Filter part
disp('Filter part 1')
fs=14.4e3;
Wp=2*pi*3/fs; %passband cutoff frequency
Ws=2*pi*12/fs; % stopband cutoff frequency
Rp=3; %passband attenuation
Rs=30; %stopband attenuation
Omip=Wp/pi; Omis=Ws/pi;
% Chebyshev LP from 0 to 6 Hz less than 3 dB ripple
%at least 30 dB from 14 Hz
Omip=Wp/pi; Omis=Ws/pi;
[Nc,Wnc] = cheb1ord(Omip,Omis,Rp,Rs);
[bc,ac] = cheby1(Nc,Rp,Wnc);
filtered = filtfilt(bc,ac,subsetAnalyzed); %Filtered signal%
afterc2 = -subsetAnalyzed+filtered;
disp('Filter part 2')
[Nc2,Wnc2]=cheb1ord(2*20/fs,2*100/fs,Rp,Rs);
[bc2,ac2]=cheby1(Nc2,Rp,Wnc2);
filtered2=filtfilt(bc2,ac2,afterc2); %Filtered signal%
afterc2=filtered2;
numPksDisp = 0;
disp('Finding peaks')
[pks, locs] = findpeaks(afterc2,'MINPEAKHEIGHT',-0.02375);
figure(3)
plot(time2,afterc2), hold on;
plot(time2(locs),pks,'k^','markerfacecolor',[1 0 0])
title('peaks identified');
pksValuec2=afterc2(time2(locs));
numPks = numPks + size(pksValuec2,1)
numPksDisp = numPks;
Can you help me solve this problem that has been plaguing me for about half of a semester? I have read countless Mathworks and Stackoverflow posts about this problem and there are no conclusive solutions available.
Here is a screenshot of the model: http://imgur.com/UkRp2ix,F6eDq6K
Here is a screenshot of my Ports and Data Manger configuration settings: http://imgur.com/UkRp2ix,F6eDq6K#1
Thank you so much for your help!

Answers (1)

Radha Krishna Maddukuri
Radha Krishna Maddukuri on 9 Jun 2015
If a MATLAB Function block is used in a model, it generates a MEX function when the model is simulated. This means that under the hood the model uses MATLAB Coder to generate the MEX function.
When using MATLAB Coder, you have the option of using dynamic memory or static memory for creating arrays. If you decide to use static memory, you can still have variable-sized arrays, but the size of the array must be bounded by some number so that we can generate code for the "worst case" of an array being as large as the bounded size. If the size is unbounded, or if the size is bounded but causes the worst case array to have more than 134217728 elements (1024 * 1024 * 128; an undocumented upper limit), an error is thrown and code generation stops. These limitations do not exist for dynamic allocation of arrays.
However, dynamic allocation of memory is not currently supported for MATLAB Function blocks, as is mentioned in the documentation here:
There are a few workarounds to consider:
1. Generate a static or dynamic library from the MATLAB function, call the 'partitionData' function with coder.ceval() in a MATLAB Function block, and link the Simulink model with the library in the Custom Code pane of Configuration Parameters. Please note that this does require a bit of effort.
2. Try to rewrite the code in such a way that the worst case static array size does not exceed the maximum limit. Right now, the line
[pks, locs] = findpeaks(afterc2,'MINPEAKHEIGHT',-0.02375);
has a computed worst case size of [:200000000 x :200000000].
The script can be re-written by breaking the arrays and computing them individually and bringing them back into MATLAB and then concatenating them.
3. Call the MEX file generated from MATLAB Coder in an Interpreted MATLAB Function block (or a regular MATLAB Function block with coder.extrinsic() ). This is not a valid option if you wish to generate code from your model.
I hope you find these workarounds useful.
  1 Comment
rizwan azam
rizwan azam on 26 Nov 2021
Thank you for a detailed answer, it helped a lot in understanding how MATLAB function block works. I am currently having an issue, probably you can shed some lite into it:
I have a matlab function named nodes inside SIMULINK, with one of the outputs named xr. xr is actually a variable sized array with maximum dimensions as 11x1. In another MATLAB function, I require to extract values with index ranging from 2:6. However SIMULINK doesn't allow me to extract this range and gives an error " An error occurred while running the simulation and the simulation was terminated Caused by:
  • Code generation assumption about size violated. Unexpected run-time vector changed predicted orientation. "

Sign in to comment.

Categories

Find more on Code Generation 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!