Is there anyway to change the code generation options(Storage class, HeaderFile attribute) automatically based on name given to the new object in model explorer?

2 views (last 30 days)
Hi all, I need some help with automation. I have created a few custom data classes for Simulink models. Now, if the user creates a new data object using those custom data classes in Model Explorer, is there anyway to automatically change the code generation options for that object based on the name assigned to that object by the user. Ideally, I would like to have the code generation options to change dynamically upon detecting a change in the name field.
Some Code:
classdef my_Parameter < Simulink.Parameter
methods
function setupCoderInfo(obj)
useLocalCustomStorageClasses(obj, 'mypkg');
obj.CoderInfo.StorageClass = 'Custom';
obj.CoderInfo.CustomStorageClass = 'Define';
obj.CoderInfo.CustomAttributes.HeaderFile = 'xyz.h';
end
end % methods
end % classdef

Answers (1)

Arnab Sen
Arnab Sen on 31 Dec 2015
I understand that you would like to set the 'Code Generation Options' based on the name of the Data Object of a Data Class.
This is possible you allow the user to rename the Data Objects with a method in the Data Class itself and in the same method you can make the appropriate changes in the 'Code Generation Options'.
For example, you may include the following method in your 'my_parameter' class definition.
function Rename(dataObj,oldName,newName)
% Update the Code generation option for the data object. a mapping can be used here to
%map 'newname' to particular option value
dataObj.CoderInfo.StorageClass='FileScope'
% Get the handle of the appropriate workspace
hws = get_param('myModel','modelworkspace');
% Create a new reference to the data object with name as 'newName'
hws.assignin(newName,dataObj);
% Remove the old reference from the particular workspace
expr=strcat('clear','oldName');
evalin(hws,expr);
end
Now User should rename the object by execution following command in MATLAB command window
>>Rename(dataObject,'oldName','newName')
Refer to the following link for more details about the functions used in above method:
  1 Comment
SP
SP on 1 Jan 2016
Thank you for your help, but unfortunately this is not quite what I am looking for.
What I am looking for is a way to automatically set the code generation options based on the name assigned to the variable. So, when a developer creates a new variable/data type for a model or changes the name of one of the already existing variable in the DD using the model explorer window, I want to set/change the code generation options for that variable accordingly.
I was thinking about this alternative approach. I can write a matlab script that parses through each object in the DD and updates the code generation options. The script would have to be called before building the model.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!