simulink matlab function dimension error

4 ビュー (過去 30 日間)
태건
태건 2025 年 9 月 22 日
コメント済み: Sam Chak 2025 年 9 月 23 日
In my first Matlab function, I set the input to a 4x1 vector, a feedback vector, and a scalar, but the output is a scalar. Please tell me how to make the output a 4x1 vector. I have attached my simulink file
  2 件のコメント
dpb
dpb 2025 年 9 月 22 日
A short reproduction using your stated dimensions
sp_des=100*rand(4,1);
sp=90;
r=0.117;
drag=0.1;
k_t=60/2/pi/960*3450;
k_e=k_t;
v=k_e.*sp_des + r.*drag.*sp.^2./k_t
v = 4×1
1.0e+03 * 0.9439 3.1334 2.6774 1.4427
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
illustrates that the result would be the 4-vector if the input variables are the desired sizes.
Therefore, it must be presumed that the variable motor_speed_des isn't atually being passed as the 4-vector, but only a scalar value from it.
Set a breakpoint in the m-function and inspect the input variables.
I am unfamiliar with Simulink so I don't know precisely how it calls the function, but something must be amiss there...
Sam Chak
Sam Chak 2025 年 9 月 23 日
Using Selector block proposed by @Angelo Yeo is technically sufficient.
Consider the Simulink block as analogous to a 4-speed table fan, where you can select only one speed at a time from a range of options. If you require a 4x1 output vector, you can use 4 "fans" or duplicate the motor block 3 additional times, followed by a vertical concatenation of the 4 individual outputs.

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

回答 (1 件)

Angelo Yeo
Angelo Yeo 2025 年 9 月 22 日
It's a variable type/dimension inheritance issue. The subsystem I marked with red rectangle expects 1-dimensional input (voltage), and Simulink tries to inherit the 1-dim dimensionality to VoltageConversion MATLAB Function's output. So, you should be changing the behavior of the subsystem in red rectangle.
For example, in the subsystem with red rectangle, you can choose which element of 4-dim vector, "voltage" to use with Selector block like below. In the case below, the second element is being used. And, see that the dimension issue is fixed for this particular input port "voltage".

カテゴリ

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

タグ

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by