Clear Filters
Clear Filters

Initialise property with simulink.bus information located in sldd

3 views (last 30 days)
Hi all
The code below is working as intended, however the 'ST_FB_TRIGGERBUFFER_DATARECORD' needs to be available in the base workspace. I have the definition stored in a data dictionary (FB_TriggerBuffer.sldd), how to change the code below, so it will search the definition in this data dictionary?
Thanks in advance
Ludo
classdef FB_TriggerBuffer < matlab.System
% System Flags
% =====================================
methods (Static, Access = protected)
function flag = supportsMultipleInstanceImpl(~)
flag = true;
end
end
% Properties declaration
% =====================================
% Public properties
properties (Access = public)
nWriteIndex uint16 = 1; % [] Index to the record which will be written
arrstDataBuffer = repmat(Simulink.Bus.createMATLABStruct('ST_FB_TRIGGERBUFFER_DATARECORD'),1,3); % [] Buffer holding the supplied data
stEmptyDataRecord = Simulink.Bus.createMATLABStruct('ST_FB_TRIGGERBUFFER_DATARECORD'); % [] Empty data record
end

Accepted Answer

Poorna
Poorna on 27 Oct 2023
Hi Ludo,
I understand that you would like to access the definition of 'ST_FB_TRIGGERBUFFER_DATARECORD' from the data dictionary 'FB_TriggerBuffer.sldd' instead of the Base workspace.
You can follow the below steps to access data from a data dictionary:
1) Declare a class property say ‘FB_TriggerBufferObj’ and initialize it with the data dictionary object using the “Simulink.data.dictionary.openfunction.Here is the sample code for the same:
FB_TriggerBufferObj = Simulink.data.dictionary.open('FFB_TriggerBuffer.sldd')
2) To access the variable in the class methods, you can use the “getSectionfunction to retrieve the section in which the entry is stored. Then, use the “getEntry” function to retrieve the entry. Finally use the “getValue” function to obtain the value of the desired entry.Here is the sample code for the same:
dataSec = getSection(FB_TriggerBufferObj, 'Design Data');
dataEntry = getEntry(dataSec, 'ST_FB_TRIGGERBUFFER_DATARECORD');
val = getValue(dataEntry);
3) Now, you can use the retrieved value as usual in the code.
To know more about the functions Simulink.data.dictionary.open”, getSection”,getEntry”, andgetValue, please refer to the following documentation.
Hope this Helps!
Best Regards,
Poorna.
  1 Comment
Ludo Houben
Ludo Houben on 27 Oct 2023
Hi Poorna
There is a small typo 'FFB_TriggerBuffer.sldd' but your answer is clear. I accepted it, although for me this solution is not working. This solution can not be used in 'code generation' simulation mode.
For others with the same issue. I solved it by running a Matlab scipt loading all used bus definitions in to base workspace.
Note: My main 'PlayGroundStructs.sldd' calls several lower level *.sldd files.
%% Load struct definition
Simulink.data.dictionary.open("PlayGroundStructs.sldd");
structEntries = find(getSection(Simulink.data.dictionary.open("PlayGroundStructs.sldd"), ...
'Design Data'), '-value', '-class', 'Simulink.Bus'); % Get all simulink.bus definitions from SLDD file
for index = 1:length(structEntries) % For all found simulink.bus definitions
assignin('base',structEntries(index).Name,getValue(structEntries(index))); % Create struct
end
clear index structEntries; % Delete temporary variables

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!