bus input into Matlab function simulink block

17 次查看(过去 30 天)
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

回答(1 个)

Kshitij Chhabra
Kshitij Chhabra 2020-6-19
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 个评论
Kshitij Chhabra
Kshitij Chhabra 2020-6-19
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 2020-6-23
Thank you Kshitij, appreciated your answers! ... and very helpful!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by