Trouble with variable-input sizes in Embedded Matlab

2 次查看(过去 30 天)
So I wrote this code that basically takes a look at the areas of different objects in two different images and then Identifies which objects correspond to the other ones based on how close they are in area. I need it for a simulink model and it runs fine in matlab. Just can you please help me, So here is the base code:
function Pairs = fcn(areas1,areas2,c)%Areas1 and Areas 2 are the areas from the retrospective clouds c is the the number of objects this is obtained from the blob analysis block in simulink%
%#codegen
count = c(:,1);
Pairs = zeros(2,count);
l = 1;
for f = 1:count
end
for e = 1:count
Constant = 50000;
for f = 1:count
d = abs(areas1(:,e) - areas2(:,f));
if d < Constant
Constant = d;
test = [e,f];
end
end
Pairs(:,l) = test;
l = l + 1;
end
And I get the following errors please help me resolve them:
Data 'Pairs' (#230) is inferred as a variable size matrix, while its specified type is something else.
MATLAB Function Interface Error: Error in port widths or dimensions. Invalid dimension has been specified for input ' SFunction '.
Then the usual parsing errors and then I also get these errors:
Class mismatch (double ~= int32). The class to the left is the class of the left-hand side of the assignment.
Function 'MATLAB Function' (#135.249.257), line 13, column 13: "Constant" Launch diagnostic report.
Any help will be extremely appreciated

回答(3 个)

Fangjun Jiang
Fangjun Jiang 2011-8-4
The number of ports and the port width of a Simulink block need to be fixed before the simulation starts. "Pairs" is the output of the function thus is treated as the outport of the Embedded MATLAB Function. However, the size of Pairs inside the code is defined as Pairs=zeros(2,count) and "count" is further determined by the input variable c. Anyway, the message is saying that you can not have this type of variable size port width.

Denis Gurchenkov
Denis Gurchenkov 2011-8-4
To fix this problem, go to Ports and Data Manager (menu Tools / Edit Data Ports), click on the line for "Pairs", check the checkbox "Variable Size", and enter the maximum size of the Pair variable in the size field.
My guess is that the size of "Pairs" should be something like
[ size(c,1) 10 ]
of course, "10" is just my guess, type in some value that is large enough to cover the number of columns in "Pairs".

Jon
Jon 2011-11-19
I'm having the same issue. What causes it to infer that it is variable size? I can get it to pass the variable size variable but the integrator block won't accept a variable size input. How can I get it to stop inferring that it is variable sized?
  1 个评论
Kaustubha Govind
Kaustubha Govind 2011-11-21
Jon: How you write your code determines whether an output is variable-size or not. If you know that your output has a fixed-size, then pre-allocate it at the beginning of the function, and avoid growing it dynamically.

请先登录,再进行评论。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by