How to fix the error 'Computed maximum size is not bounded. Static memory allocation requires all sizes to be bounded. '?
80 次查看(过去 30 天)
显示 更早的评论
Hello everyone, I want to define my own function in Simulink. But when I run my function, it gets the following error,
And below is my function's source code,
function path = pathfinder3_1( R,D,start_node,end_node )
%Find path in points matrix
% Initialization begins----------------------
a = size(R,1);
node_name = zeros(a(1),1);
for i1 = 1:a
node_name(i1) = i1;
end
current_node = start_node;
unvisited = node_name;
unvisited(current_node) = NaN;
coder.varsize('temp',[1 a]);
coder.varsize('temp_t',[1 a]);
coder.varsize('temp_name',[1 a]);
temp = current_node;
temp1 = [];
past_node = NaN(12,1);
node_value = Inf(a,1);
node_value(start_node) = 0;
% Initialization ends-------------------------
% While loop----------------------------------
while 1
% current_node
temp_size = size(temp,2);
min_value = Inf;
min_node = [];
for i3 = 1:temp_size
if node_value(temp(i3)) < min_value
min_value = node_value(temp(i3));
min_node = temp(i3);
end
end
% min_node
current_node = min_node;
unvisited(current_node) = NaN;
temp1 = R(current_node,1:end);
temp_size1 = 0;
for j1 = 1:a
if temp1(j1) ~= 0
temp_size1 = temp_size1+1;
% temp = [temp temp1(j1)];
end
end
q2 = temp_size+temp_size1;
coder.varsize('temp_t',[1 a]);
temp_t = zeros(1,q2);
for j5 = 1:temp_size
temp_t(j5) = temp(j5);
end
for j2 = 1:temp_size1
temp_t(j2+temp_size) = temp1(j2);
end
% temp
for i2 = 1:temp_size1
t1 = node_value(current_node)+D(current_node,temp1(i2));
if node_value(temp1(i2)) > t1
node_value(temp1(i2)) = t1;
past_node(temp1(i2)) = current_node;
end
end
temp2 = temp_t;
p = size(temp2,2);
temp_name = zeros(1,p);
for i4 = 1:p
if isnan(unvisited(temp2(i4)))
temp2(i4) = NaN;
end
end
% temp = [];
temp_size2 = 0;
for i6 = 1:p
if ~isnan(temp2(i6))
% temp = [temp temp2(i6)];
temp_size2 = temp_size2+1;
temp_name(temp_size2) = temp2(i6);
end
end
% temp_name
temp = zeros(1,temp_size2);
for j4 = 1:temp_size2
temp(j4) = temp_name(j4);
end
% unvisited
% node_value
% past_node
% temp
if isempty(temp)
break;
end
end
% while loop ends-----------------------------
% Find path-----------------------------------
coder.varsize('path1');
path = zeros(1,a);
path(a) = end_node;
i5 = end_node;
l = a;
while 1
a = a-1;
path(a) = past_node(i5);
i5 = past_node(i5);
if path(a) == start_node
break;
end
end
% k = l-a+1;
% path1 = zeros(1,k);
% for j5 = 1:k
% path1(j5) = path(a);
% a = a+1;
% end
% Find path ends------------------------------
end
1 个评论
YASSINE OUDEDDAR
2019-11-19
编辑:YASSINE OUDEDDAR
2019-11-30
To solve this problem, it is necessary that the size used by each variable used in MATLAB Function is not dynamic, for this purpose it is necessary to set the size of the variables used with fixed values.
For example: are a one of the entries of the MATLAB Function of the integer type and which can vary in each executed task, and Y and Z two variables used in this block: if we write for example Y=2:a:200 or Z=linspace(2-a,400,40*a) in MATLAB Function it will not work because the entry a can vary the size used by Y and Z in each task. So to solve this we have to be satisfied with a fixed number of elements of Y and Z; then let's note N1 and N2 (with fixed values) respectively the length of Y and Z then write Y=linspace(2,200,N1) and Z=linspace(2-a,400,N2) and it will work.
Note: to use X=start:step:end, sometimes a dynamic step can be chosen instead of a fixed one, but at least one of the boundaries of the variable X must also be dynamic so that the size of the variable X is fixed & non dynamic.
回答(3 个)
Abdullah
2019-7-9
Try this
temp_t = zeros(1,int8(q2));
instead of
temp_t = zeros(1,q2);
I do not know the reason exactly but it works for me.
1 个评论
Arnav Mendiratta
2017-6-13
For a MATLAB Function block, you need to explicitly define the sizes and upper bound in order to use variable size signals. Go to the "Edit Data" option of the MATLAB Function from the Editor toolstrip and check the variable size box. Also, make sure you have specified upper bounds as described in the the documentation:
I am not sure what the function input and output mean, but let's say if you expect the 'path' output to be a a variable size array, you must specify the maximum number of elements you expect in this array. For example specify [1 10] if the anticipated size of 'path' cannot exceed 10.
Similarly, for all the variables in the code, you must explicitly specify the sizes. In your code, temp, temp_t, q2 are all ambiguous, which is why you receive this error. Again, I am unable to run your code because I don't know what the input and outputs look like, but if I replace these variables with explicit definitions like:
temp = 0;
or,
temp_t = zeros(1,100)
These errors go away.
YASSINE OUDEDDAR
2019-11-19
编辑:YASSINE OUDEDDAR
2019-11-20
To solve this problem, it is necessary that the size used by each variable used in MATLAB Function is not dynamic, for this purpose it is necessary to set the size of the variables used with fixed values.
For example: are a one of the entries of the MATLAB Function of the integer type and which can vary in each executed task, and Y and Z two variables used in this block: if we write for example Y=2:a:200 or Z=linspace(2-a,400,40*a) in MATLAB Function it will not work because the entry a can vary the size used by Y and Z in each task. So to solve this we have to be satisfied with a fixed number of elements of Y and Z ; then let's note N1 and N2 (with fixed values) respectively the length of Y and Z then write Y=linspace(2,200,N1) and Z=linspace(2-a,400,N2) and it will work.
Note: to use Y=start:step:end, sometimes a dynamic step can be chosen instead of a fixed one, but at least one of the boundaries of the variable Y must also be dynamic so that the size of the variable Y is fixed & non dynamic.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Inputs 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!