Download data dictionary programmatically

Hi, I would like to download data dictionary linkied to a simulink model programmatically and save it in a preferred location. Can anyone let me knoe how its done?

Antworten (1)

Rahul
Rahul am 3 Sep. 2024

0 Stimmen

Hi Harish,
I understand you are unable to programmatically download the data dictionary associated to a Simulink model and save it to a preferred location locally.
You can use the Simulink API to interact with the model and its associated data dictionary. Here’s a possible solution to the issue you’re facing:
Steps to Download and Save a Data Dictionary:
  • Open the Simulink Model: Ensure that your Simulink model is open or load it programmatically.
  • Access the Data Dictionary: Use the 'get_params' function to find and access the data dictionary linked to the model.
  • Save the Data Dictionary: Save the data dictionary to your preferred location.
Here's an example script to illustrate these steps:
% Load Simulink Model
modelName = 'your_model_name';
load_system(modelName);
% Get the data dictionary associated with the model
ddInfo = get_param(modelName, 'DataDictionary');
% Check if the model uses a data dictionary
if ~isempty(ddInfo)
% Open the data dictionary
ddObj = Simulink.data.dictionary.open(ddInfo);
% Specify the path where you want to save the data dictionary
savePath = 'C:\path\to\your\preferred\location\your_data_dictionary.sldd';
% Save the data dictionary and close the object
saveAs(ddObj, savePath);
close(ddObj);
disp(['Data dictionary saved to: ', savePath]);
else
disp('The model does not use a data dictionary.');
end
close_system(modelName, 0);
  • Model Name: Replace 'your_model_name' with the actual name of your Simulink model.
  • Save Path: Specify the full path where you want to save the data dictionary, replacing 'C:\path\to\your\preferred\location\your_data_dictionary.sldd'.
For more information regarding programmatically extracting model parameters, refer to the documentation below:

5 Kommentare

Harish
Harish am 3 Sep. 2024
Bearbeitet: Harish am 3 Sep. 2024
Hi Rahul,
thanks for the answer. This code is partly working till creation of ddObj. However, I am facing an issue with using saveas. The error says "Error using saveas, Invalid handle"
(I tried both, saveas and saveAs)
Hey @Harish,
A possible workaround for this can be done by creating another Simulink DataDictionary Object in the desired location, assign the value of our Model's DataDictionary Object to this object, then modify and save all changes. You can try adding the following snippet after instead of the 'saveas' function:
newDataDict = Simulink.data.dictionary.create(savePath)
% Perform modifications to newDataDict object
...
saveChanges(newDataDict)
Harish
Harish am 3 Sep. 2024
Thankyou. Can you also post how to copy ddObj to newly created data dictionary?
Rahul
Rahul am 4 Sep. 2024
Preferrably, an assignment operation before saving changes, would suffice:
newDataDict = ddObj;
Harish
Harish am 4 Sep. 2024
Bearbeitet: Harish am 4 Sep. 2024
Hi Rahul,
I tried both, unfortunately it is not reflecting on the original file. The variable newDataDict is assigned the new values inside the workspace as expected, but it is not being reflected on the file newly created.

Melden Sie sich an, um zu kommentieren.

Produkte

Version

R2023b

Gefragt:

am 3 Sep. 2024

Bearbeitet:

am 4 Sep. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by