bus input into Matlab function simulink block
3 views (last 30 days)
Show older comments
Hello everyone,
I want to use a bus signal as input for a matlab function simulink block. I created a simple example, here we go:

the matlab function looks like:

the error message looks as:

I'm sure there is an easy way to solve it, what is the problem and how to overcome it??
many thanks and best greetings,
Stefan
0 Comments
Answers (1)
Kshitij Chhabra
on 19 Jun 2020
Hi Stefan,
From my understanding of the question you are stuck because you want to access the signals from a Simulink Bus but weren’t able to do so. The possible source of error is because you didn’t create a Bus object in the base workspace.
You can try running this script to make a bus object after modifying it according to your needs:
% Change Bus1 to whatever name you want to give
Bus1 = Simulink.Bus;
Bus1.Description = '';
Bus1.DataScope = 'Auto';
Bus1.HeaderFile = '';
Bus1.Alignment = -1;
% Signal 1
saveVarsTmp{1} = Simulink.BusElement;
% Name of Signal 1
saveVarsTmp{1}.Name = 'x1';
saveVarsTmp{1}.Complexity = 'real';
saveVarsTmp{1}.Dimensions = [1 1];
% Data Type
saveVarsTmp{1}.DataType = 'uint32';
saveVarsTmp{1}.Min = [];
saveVarsTmp{1}.Max = [];
saveVarsTmp{1}.DimensionsMode = 'Fixed';
saveVarsTmp{1}.SamplingMode = 'Sample based';
saveVarsTmp{1}.SampleTime = -1;
saveVarsTmp{1}.DocUnits = '';
saveVarsTmp{1}.Description = '';
%Signal 2
saveVarsTmp{1}(2, 1) = Simulink.BusElement;
saveVarsTmp{1}(2, 1).Name = 'x2';
saveVarsTmp{1}(2, 1).Complexity = 'real';
saveVarsTmp{1}(2, 1).Dimensions = [1 1];
saveVarsTmp{1}(2, 1).DataType = 'uint32';
saveVarsTmp{1}(2, 1).Min = [];
saveVarsTmp{1}(2, 1).Max = [];
saveVarsTmp{1}(2, 1).DimensionsMode = 'Fixed';
saveVarsTmp{1}(2, 1).SamplingMode = 'Sample based';
saveVarsTmp{1}(2, 1).SampleTime = -1;
saveVarsTmp{1}(2, 1).DocUnits = '';
saveVarsTmp{1}(2, 1).Description = '';
Bus1.Elements = saveVarsTmp{1};
clear saveVarsTmp;
After this assign the Bus1 data type to Output Data Type of the created bus.
3 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!