Dimension 1 is fixed on the left-hand side but varies on the right ([1 x 1] ~= [:? x 1]).
9 次查看(过去 30 天)
显示 更早的评论
Good morning,
I'm writing a piece of code in a simulink block. Here the code
function [otv1, otv2, otv3] = TCU(P_tank1, P_tank2, P_tank3, time)
%% input
toll = 1; %[bar]
% coder.varsize('otv1');
% coder.varsize('otv2');
% coder.varsize('otv3');
persistent OTV1 OTV2 OTV3
if isempty(OTV1); OTV1 = 0; end
if isempty(OTV2); OTV2 = 0; end
if isempty(OTV3); OTV3 = 0; end
if time <= 0
OTV1 = 0;
OTV2 = 0;
OTV3 = 0;
else
Tank_unsorted =[P_tank1 OTV1
P_tank2 OTV2
P_tank3 OTV3];
% ordine decrescente di pressione
[Tank, index] = sortrows(Tank_unsorted, 'descend');
if (Tank(1,1) - Tank(2,1)) >= toll
Tank(1,2) = 1;
Tank(2,2) = 0;
Tank(3,2) = 0;
else if (Tank(1,1) - Tank(2,1)) < toll
Tank(1,2) = 1;
Tank(2,2) = 1;
Tank(3,2) = 0;
else if (Tank(2,1) - Tank(3,1)) <= toll
Tank(1,2) = 1;
Tank(2,2) = 1;
Tank(3,2) = 1;
end
end
end
end
index_otv = find(index == 1);
OTV1 = Tank(index_otv, 2);
index_otv = find(index == 2);
OTV2 = Tank(index_otv, 2);
index_otv = find(index == 3);
OTV3 = Tank(index_otv, 2);
%% output
otv1 = OTV1;
otv2 = OTV2;
otv3 = OTV3;
I tried the logic of the code in a script, and it works: OTVn in the end contains the correct scalar. But during the execution i have the error I wrote in the title at the lines
OTV1 = Tank(index_otv, 2);
and similar. But actually OTVn contains a scalar, because it's extracted from the matrix Tank.
I have tried the solutions on the net, but none of them worked (including coder.varsize). How could i overcome the problem? Considering the goal is to generate code
0 个评论
回答(1 个)
Infinite_king
2024-5-10
编辑:Infinite_king
2024-5-10
Hi Federico Avino,
The MATLAB Coder was unable to determine that the variable 'index_otv' would always be a scalar, leading to the error.
While analyzing the expression 'index_otv = find(index == 1)', MATLAB Coder considers the type and size of the variable 'index' but does not account for the values stored in the variable. Since 'index' can contain any values, MATLAB Coder concludes that the function call 'find(index == 1)' may return a vector.
To resolve the issue, you can first store the result in a temporary variable, 'tOTV,' and then extract the value at index 1 and store it in the variable 'OTV'.
Please find the updated code below,
function [otv1, otv2, otv3] = TCU(P_tank1, P_tank2, P_tank3, time) %#codegen
%% input
toll = 1; %[bar]
% coder.varsize('otv1');
% coder.varsize('otv2');
% coder.varsize('otv3');
persistent OTV1 OTV2 OTV3
if isempty(OTV1); OTV1 = 0; end
if isempty(OTV2); OTV2 = 0; end
if isempty(OTV3); OTV3 = 0; end
if time <= 0
OTV1 = 0;
OTV2 = 0;
OTV3 = 0;
else
Tank_unsorted =[P_tank1 OTV1
P_tank2 OTV2
P_tank3 OTV3];
% ordine decrescente di pressione
[Tank, index] = sortrows(Tank_unsorted, 'descend');
if (Tank(1,1) - Tank(2,1)) >= toll
Tank(1,2) = 1;
Tank(2,2) = 0;
Tank(3,2) = 0;
else if (Tank(1,1) - Tank(2,1)) < toll
Tank(1,2) = 1;
Tank(2,2) = 1;
Tank(3,2) = 0;
else if (Tank(2,1) - Tank(3,1)) <= toll
Tank(1,2) = 1;
Tank(2,2) = 1;
Tank(3,2) = 1;
end
end
end
%------------------------------------------------------------------------------------%
% Change 1 - Moved the code inside the for loop.
% Change 2 - Values are stored in temporary variables
% and then transferred to main variables.
index_otv = find(index == 1);
tOTV1 = Tank(index_otv, 2);
index_otv = find(index == 2);
tOTV2 = Tank(index_otv, 2);
index_otv = find(index == 3);
tOTV3 = Tank(index_otv, 2);
OTV1 = tOTV1(1);
OTV2 = tOTV2(1);
OTV3 = tOTV3(1);
%------------------------------------------------------------------------------------%
end
%% output
otv1 = OTV1;
otv2 = OTV2;
otv3 = OTV3;
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!