How to programmatically create Simulink.Parameter object from nested bus in data dictionary?
Mostra commenti meno recenti
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
- Create these objects in base workspace, i.e. create a Simulink.Parameter named 'Parameter' with the bus structure 'UDT_Parameter'
- Update simulation values (parameters)
- 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.
- I open the data dictionary, open Type Editor, right click on the desired nested bus and select 'Create Simulink.Parameter Object'
- The output is i.e. 'ParameterObject.m' containing the entire nested sturcture.
- I then delete the last part in 'ParameterObject.m', as the desired object names are given in the next script.
- 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



Risposta accettata
Più risposte (2)
Ruchika Parag
il 9 Lug 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
il 10 Lug 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!
Categorie
Scopri di più su Code Interface Configuration and Integration in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!