bus input into Matlab function simulink block

17 views (last 30 days)
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

Answers (1)

Kshitij Chhabra
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
Kshitij Chhabra
Kshitij Chhabra on 19 Jun 2020
Hi again,
Bus definitions are often necessary whenever you make a custom function or a system object. The reason for the same being that Simulink Blocks knows what type of data as input would be coming (they are programmed accordingly) wheras in the case of UDF you'll have to specify the same which is done by making the bus object.
Stefan Haarhoff
Stefan Haarhoff on 23 Jun 2020
Thank you Kshitij, appreciated your answers! ... and very helpful!

Sign in to comment.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!