How to pass vector variable in Simulink Bus?
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
madhan ravi
le 24 Nov 2023
Modifié(e) : madhan ravi
le 24 Nov 2023
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 commentaire
madhan ravi
le 24 Nov 2023
Modifié(e) : madhan ravi
le 24 Nov 2023
updating the model shortcut: ctrl + d in windows
Voir également
Catégories
En savoir plus sur Interactive Model Editing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!