error in Simulink matlab function

3 ビュー (過去 30 日間)
tianyuan wang
tianyuan wang 2025 年 3 月 21 日
回答済み: Walter Roberson 2025 年 3 月 22 日
Hi
I encounter a problem. Matlab Function allows the matrix transposition:
y = fun(x) % x = [6,1]
x = x';
x
end
For my codes:
bPosGlobal = zeros(6,1)
function [y] = OWScalcBodyWave(bPosGlobal)
y = zeros(6,1);
rowNum = size(bPosGlobal,1)
columnNum = size(bPosGlobal,2)
if (rowNum < columnNum && rowNum > 1) || (rowNum > columnNum && columnNum == 1)
bPosGlobal = bPosGlobal'; % Must be row vector
end
bPosGlobal
end
It reports:
Size mismatch between two arrays
Size mismatch (size [6][1] ~= size [1][6])
But when I comment out some codes, the error disappear:
function [y] = OWScalcBodyWave(bPosGlobal)
y = zeros(6,1);
rowNum = size(bPosGlobal,1)
columnNum = size(bPosGlobal,2)
% if (rowNum < columnNum && rowNum > 1) || (rowNum > columnNum && columnNum == 1)
bPosGlobal = bPosGlobal'; % Must be row vector
% end
bPosGlobal
end
Why this happens?
  2 件のコメント
Sam Chak
Sam Chak 2025 年 3 月 21 日
Hi @tianyuan wang, by clicking the indentation icon , you can copy and paste the code in a format that resembles the MATLAB Editor environment.
Chuguang Pan
Chuguang Pan 2025 年 3 月 22 日
bPosGlobal = zeros(6,1);
OWScalcBodyWave(bPosGlobal);
rowNum = 6
columnNum = 1
bPosGlobal = 1×6
0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
function [y] = OWScalcBodyWave(bPosGlobal)
y = zeros(6,1);
rowNum = size(bPosGlobal,1)
columnNum = size(bPosGlobal,2)
if (rowNum < columnNum && rowNum > 1) || (rowNum > columnNum && columnNum == 1)
bPosGlobal = bPosGlobal'; % Must be row vector
end
bPosGlobal
end

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2025 年 3 月 22 日
Simulink needs to be able to calculate the output size at compile time. Simulink is able to take into account formulas such as size(VARIABLE,DIMENSION) . However a condition such as
if (rowNum < columnNum && rowNum > 1) || (rowNum > columnNum && columnNum == 1)
is too complicated for it to be able to calculate the output size at compile time.
I think what you want to do is going to be difficult.
I think you should constrain the input to be appropriately sized and let that size propagate back.
Or consider using a Transopose block https://www.mathworks.com/help/simulink/slref/transpose.html possibly with an if/then/else block structure https://www.mathworks.com/help/simulink/slref/if.html

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgrammatic Model Editing についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by