How to programmatically create Simulink.Parameter object from nested bus in data dictionary?

8 views (last 30 days)
Hi.
I have a data dictionary with multiple nested busses, that I currently manually create Simulink.Parameter objects from, to be able to use the following script functions to
  1. Create these objects in base workspace, i.e. create a Simulink.Parameter named 'Parameter' with the bus structure 'UDT_Parameter'
  2. Update simulation values (parameters)
  3. Copy the objects (i.e. 'Parameter') in the data dictionary for usage in simulink models.
The bus sturctures are created according the external needs and it is used by PLC Coder to generate the desired UserDefinedType's (UDT) in a PLC project.
The manual process is blocking from developing automated processes to update all the objects and multiple files (.csv and .xlsx) when changes are made:
Let me show the process, and you might be able to help with some command(/script process) that I have not found yet.
  1. I open the data dictionary, open Type Editor, right click on the desired nested bus and select 'Create Simulink.Parameter Object'
  2. The output is i.e. 'ParameterObject.m' containing the entire nested sturcture.
  3. I then delete the last part in 'ParameterObject.m', as the desired object names are given in the next script.
  4. Lastly i run the script 'UpdateParameters.m', which open all object creation .m files, set the desired object names, edit to simulation values and updates the parameter objects in the dictionary (i.e 'Parameter').
I would very much like to find a command that can create the Simulink.Parameter object .m file. The rest I already have in parts and the deletion of the last x lines is no problem. But I can't find anything in the documentation on how to do step 1.
So, help please

Accepted Answer

Steven
Steven on 10 Jul 2025
Thank you for your answers @Ruchika Parag and @Suraj Kumar.
Yesterday, while researching another data extraction issue, I stumbled over the following command:
I managed to achieve the desired result, by using the same approach as you suggested @Suraj Kumar, but using the createMATLABStruct command instead of your step 2. So, one of my Simulink.Parameter objects is created like this:
TempStruct = Simulink.Bus.createMATLABStruct('UDT_Parameter',[],1,dictionaryObj); % Create struct from nested bus in Data Dictionary
Parameter = Simulink.Parameter;
Parameter.Value = TempStruct;
Parameter.CoderInfo.StorageClass = 'ExportedGlobal'; % Setting for PLC Coder
Parameter.DataType = 'Bus: UDT_Parameter';
setValue(getEntry(sectionObj,'Parameter'), Parameter); % Write object in DD
This morning, I test your step 2:
myStructData = getValue(getEntry(dData, 'myStructData'));
and that didn't work out of the box, as myStructData is a Simulink.Bus. I received this error message:
Invalid value specified for parameter. Value must be a numeric array, fi object, enumerated value, structure whose fields
contain valid values, string scalar, or an expression.
So that must be converted to at struct to work. But createMATLABStruct works as a one line command, so I'll stick to that.

More Answers (2)

Ruchika Parag
Ruchika Parag on 9 Jul 2025
Hi @Steven, you're correct that the "Create Simulink.Parameter Object" option from the Type Editor is very useful for quickly generating parameter definitions based on nested bus structures. However, there's currently no documented API that reproduces this exact functionality (i.e., generating the .m script automatically from a bus type via command line).
That said, you can achieve the same result programmatically by creating a Simulink.Parameter object, assigning a struct that matches your bus hierarchy, and writing that definition into a .m file. Here's a simplified example:
% Define a nested structure that matches your bus definition
myStruct = struct;
myStruct.General = struct;
myStruct.General.dummyTag = 0;
myStruct.General.ServerIP = int32(0);
myStruct.ActiveControl = struct;
% Create Simulink.Parameter object
Parameter = Simulink.Parameter;
Parameter.Value = myStruct;
Parameter.DataType = 'Bus: UDT_Parameter';
Parameter.CoderInfo.StorageClass = 'ExportedGlobal';
% Save as .m file (mimicking the GUI behavior)
filename = 'ParameterObject.m';
fid = fopen(filename, 'w');
fprintf(fid, 'myStruct = struct;\n');
fprintf(fid, 'myStruct.General = struct;\n');
fprintf(fid, 'myStruct.General.dummyTag = 0;\n');
fprintf(fid, 'myStruct.General.ServerIP = int32(0);\n');
fprintf(fid, 'myStruct.ActiveControl = struct;\n\n');
fprintf(fid, 'Parameter = Simulink.Parameter;\n');
fprintf(fid, 'Parameter.Value = myStruct;\n');
fprintf(fid, 'Parameter.DataType = ''Bus: UDT_Parameter'';\n');
fprintf(fid, 'Parameter.CoderInfo.StorageClass = ''ExportedGlobal'';\n');
fclose(fid);
This script builds the .m file just like the one generated by the GUI. If needed, you can automate this further by looping through multiple bus definitions and creating corresponding .m files.
Also, if you're working with a data dictionary, you can add the parameter directly to it:
dictObj = Simulink.data.dictionary.open('yourDictionary.sldd');
dData = getSection(dictObj, 'Design Data');
addEntry(dData, 'Parameter', Parameter);
saveChanges(dictObj);
Although it’s not a one-command replacement for the GUI feature, this approach can help you fully automate the creation and management of structured parameters for code generation workflows. Hope it helps!

Suraj Kumar
Suraj Kumar on 10 Jul 2025
Hi Steven,
To programmatically create Simulink.Parameter object from a nested bus in data dictionary, please refer to the following steps and the attached code snippets:
1. Open the data dictionary from the command line and access the "Design Data" section where the variables are stored:
dictObj = Simulink.data.dictionary.open('myDictionary.sldd');
dData = getSection(dictObj, 'Design Data');
2. Retrieve the struct and the bus type from the dictionary to use them for creating the parameter object:
myStructData = getValue(getEntry(dData, 'myStructData'));
3. Then, create the Simulink.Parameter object in MATLAB:
myStructParam = Simulink.Parameter;
myStructParam.Value = myStructData;
For more information on the 'getSection' and 'getValue' functions in MATLAB, please refer to the following documentation:
Happy Coding!

Categories

Find more on Code Interface Configuration and Integration in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!