How to pass vector variable in Simulink Bus?
조회 수: 6 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
답변 (1개)
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
2023년 11월 24일
편집: madhan ravi
2023년 11월 24일
updating the model shortcut: ctrl + d in windows
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Model Editing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!