bus input into Matlab function simulink block

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)

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

Thank you very much, that did the job.
Can you say something about "why is this bus definition necessary"? Don't get it really. Simulink knows the properties of this bus without the definition. (If I use only simulink blocks I do not need a bus definition.) Why is this bus definition not done automatically by simulink when needed, e.g. in case of using matlab function block?
Thanks again,
Stefan
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.
Thank you Kshitij, appreciated your answers! ... and very helpful!

Sign in to comment.

Products

Release

R2018b

Asked:

on 18 Jun 2020

Commented:

on 23 Jun 2020

Community Treasure Hunt

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

Start Hunting!