How to pass vector variable in Simulink Bus?

4 ビュー (過去 30 日間)
Karol P.
Karol P. 2023 年 11 月 1 日
編集済み: madhan ravi 2023 年 11 月 24 日
Hi! I'm looking for a method to pass vector variable as output of MATLAB Function block in Simulink. Let's say that I have a structure that I wan to pass and it contains only scalar variables:
var_struct=struct('A',A,'B',B,'C',C);
I set the Bus to hold three variables of type double and I can send it this way to other model blocks. But now Let's add one more variable, vector:
vecD=[1 2 3 4 5]
var_struct=struct('A',A,'B',B,'C',C,'vecD',vecD);
It will end with error:
Dimension 2 of field 'vecD' is fixed on the left-hand side but varies on the right ([1 x 1] ~= [1 x :?]).
Of course I can set it to variable size in bus (Type Editor), and set 2-dimmensonal but in this case I get the error:
Size mismatch error on dimension 2: expected 1, but actual size is 5
How to change the size expected by the bus?

回答 (1 件)

madhan ravi
madhan ravi 2023 年 11 月 24 日
編集済み: madhan ravi 2023 年 11 月 24 日
Create Bus Object:Inside the MATLAB Function block, define the bus object using the Simulink.Bus.createObject function. This should be done before you use it as an output.
function output = myFunction(input)
% Create bus object
busObject = Simulink.Bus.createObject(input);
% Define fields for the bus object
busObject.Elements(1).Name = 'A';
busObject.Elements(2).Name = 'B';
busObject.Elements(3).Name = 'C';
busObject.Elements(4).Name = 'vecD';
% Set vector size for variable-size signals
busObject.Elements(4).Dimensions = [-1 1];
% Create the bus signal
output = busObject;
Update MATLAB Function:
%Continue with your processing code and assign values to the fields of the bus object.
% Your processing code here
% Assign values to the bus object fields
output.A = ...; % Your result for A
output.B = ...; % Your result for B
output.C = ...; % Your result for C
output.vecD = ...; % Your result for vecDSet Bus Output:In the MATLAB Function block parameters, go to the "Output" tab.Set the "Output data type" to "Inherit: auto."Update Bus Usage in Model:Connect the bus object to your MATLAB Function block's output and other blocks in your model.
  1 件のコメント
madhan ravi
madhan ravi 2023 年 11 月 24 日
編集済み: madhan ravi 2023 年 11 月 24 日
updating the model shortcut: ctrl + d in windows

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

カテゴリ

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

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by