Bug and Fix for "Inferred size for data does not match specified size" error

64 views (last 30 days)
I found this huge bug on my Simulink, and I also saw online a lot of unanswered question about this topic. I was also lucky enough to find a fix. This is my description of the problem and the solution.
Part 0: Disclaimer. Generally if you get the error "Inferred size for data does not match specified size", just check your signals dimensions, if you maybe need to transpose them, etc., becasue 99% of the times is a mistake you made and not this bug.
Part 1: Bug description.
I have written a matlab function block to allign 2 signals into only one, and I made it to work based on input signals dimensions, because these signals dimension are parametrized by a Matlab script. This is the code, and it is correct and tested:
function out = mat_align(in1, in2)
size1 = size(in1);
size2 = size(in2);
dim = length(in1);
%
out = zeros(dim, (size1(2)+size2(2)));
%
for i=1:dim
for j=1:size1(2)
out(i, j) = in1(i, j);
end
for k=1:size2(2)
out(i, (size1(2)+k)) = in2(i, k);
end
end
The bug is (at least, I didn't make more tests) present if you connect a block like this, where output signal depend on input, to a "Manual Switch" block.
Part 2: How the bug presents.
I had as mat_align input signal a 9x1 and a 9x3, and as a result, the output is a 9x4. Of course the other input to the manual switch was a 9x4 as well, and in particular a constant matrix. I then STOPPED the simulation, and changed the parameter in the Matlab script, cleared and lauched all, and started the simulation on Simulink. Now the inputs were 9x1 and 9x2, resulting in a 9x3 output, and also the other manual switch input was a constant 9x3 as well (it also modify automatically). But the simulation doesn't start and gives the error "Inferred size for data does not match specified size". Of course everything is correct, and function properly if not connected, but as soon as I connect it doesn't work, as if simulink SAVE THE OUTPUT DIMENSION AS FIXED.
Part 3: How to solve the bug.
Since it seemed a "cache problem", I tred to clear Simulink cache with: Simulink.sdi.clear, but nothing changed.
While testing, I found out that to solve the bug, after you change the input signals dimension parameters you need to:
  1. unconnect the matlab function block from the manual switch
  2. CHANGE the name of the function, e.g. instead of mat_align you call it mat_align_v2 (THIS is a really important point)
  3. Start the simulation. Then Stop it. This is done to "update" the "simulink cache"
  4. Connect the matlab function block again to the manual switch and DO NOT change the name
  5. enjoy (with a different name ^^)
Of course I hope that the Simulink team will fix the problem because I don't want to change the name every time and do the procedure for all the blocks, but for now if you need a fast fix, there it is.
I hope you find this useful.
Eventine.
  6 Comments
Fangjun Jiang
Fangjun Jiang on 18 Aug 2022
I doubt this is a bug. If you disagree, you could send your example to the Mathworks tech support to confirm.
If you want to use MATLAB Function block, you need to deal with the variable size data properly.
The "Vector Concatenate" block can be used to replace your MATLAB Function block. You also need to deal with the variable size data though.
laiche zeghdi
laiche zeghdi on 19 Sep 2023
i have this problem how can fix this (Inferred size ('scaler')) for data 'Sa' does not match specified size ('[2]')

Sign in to comment.

Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!