I'm generating a signal from a Stateflow chart in Simulink R2016a that will be connected to a real-time machine and force sensor, but for now, I'm just debugging with a sine wave. The size of the signal that is ultimately output in Stateflow is a 6 x 5 array. However, when I send it to the base workspace using the To Workspace Simulink block, I get a much larger array once the simulation is complete. Specifically, when I send it to a 2D array, it was instead a 91056 x 5 2D array mostly full of zeros.
Out of curiosity, I changed it to output 2D arrays as 3D arrays, and it was a 6 x 5 x 15176 array, and only the last 2D array had non-zero columns.
This indicates that I had made an oversight in my Stateflow chart, and the array is output at every step. I would rather preserve the very last 2D array since I will need only that for later calculations during the simulation (although I am only interested in this first half).
Is there an easy way to preserve only the last array using Simulink blocks once the first half of the simulation is over to use that during the second half, or will I need to develop a clever Matlab user-defined function block?
I've tried creating a function that I thought would work, but I'm not having any luck reducing the size. In fact, x_in and x_out are the same size at the end of the simulation.
function x_out = fcn(x_in)
K = size(x_in,3);
x_out = zeros(6,5);
for k=1:K
if all(x_in(:,1,k)==0) ||...
all(x_in(:,2,k)==0) ||...
all(x_in(:,3,k)==0) ||...
all(x_in(:,4,k)==0) ||...
all(x_in(:,5,k)==0)
x_out = x_in(:,:,k);
else
x_out = x_in(:,:,k);
end
end
Addendum I just tried to use a simpler Matlab function block that runs
x_out = x_in(x_in~=0);
and then I put the signal through a reshape block. 
It did not give the desired result and here is the error message.
'x_out' is inferred as a variable-size matrix, but its size is specified as inherited or fixed. Verify 'x_out' is defined in terms of non-tunable parameters, or select the 'Variable Size' check box and specify the upper bounds in the Size box.
I've had similar errors trying to debug my code, and I had set the size of the outputs. But I tried to do that here and the code wouldn't compile still.