Is there a way to generate a simulink project template programmatically without including referenced projects?

8 次查看(过去 30 天)
I am trying to generate a simulink project template programmatically without including reference projects. This option is available when using the Share -> Simulink Project method.
I've tried this with the Simulink.exportToTemplate method but the documentation doesn't describe a parameter to not include referenced projects. Is there a way to achieve this?

采纳的回答

Eugene Ofori
Eugene Ofori 2023-12-19
编辑:Eugene Ofori 2023-12-19
I found a workaround. The template keeps referring to their reference projects but won't package them into the template if the reference projects can't be found on the specified path when creating it. So my method involves:
  1. copying the project to the 'tempdir', (such that the reference projects won't be on path)
  2. opening the copied project and creating the template,
  3. cleaning up by deleting the copied content.
oldpath = path;
newroot = fullfile(tempdir, 'SimulinkTempDump')
og_proj_root = folder_nm;
mkdir(newroot);
copyfile(og_proj_root, newroot)
temp_proj = openProject(newroot);
Simulink.exportToTemplate(...
temp_proj, ...
template_path, ...
'Description', description, ...
'Title', template_nm);
%% Cleanup
close(temp_proj);
openProject(og_proj_root);
path(oldpath) %replace the old path so we can delete our temp folder
%% Remove Temp
if exist(newroot, 'dir')
rmdir(newroot, 's');
end
Note:
  1. Since the reference projects are not successfully opening on the copied project, I'm not sure what issues this may cause when creating the template.
  2. If your project uses absolute paths for its references or the copied project (using relative paths) can still find the reference projects, I think your reference projects will still be packaged with your template.

更多回答(1 个)

Infinite_king
Infinite_king 2023-9-26
Hi,
I understand that you want to generate a project template without including project references by using “Simulink.exportToTemplate” function.
One way of achieving this is to remove the referenced projects from the main project using “removeReference” function and then generating a project template using “Simulink.exportToTemplate” function.
Please refer the below template code,
%create a project and add the model
proj = matlab.project.createProject(ProjectPath); % Replace the ProjectPath with the desired path where you want to create the project
proj.Name = "ProjectName"; % rename the project
addFile(proj,'modelName.slx') % add the required model to the project
% or Open the project
proj = openProject(ProjectPath); % replace the ProjectPath with the path of the project
removeReference(proj,PathtoReferencedProject); % give path of refernced project to remove the reference from the project
templatefile = Simulink.exportToTemplate(proj,TemplateName); % Converting the project to a template
For more information on how to create project, add files and create project template, refer the following resources
  1. https://in.mathworks.com/help/matlab/ref/matlab.project.createproject.html
  2. https://in.mathworks.com/help/matlab/ref/matlab.project.project.addfile.html
  3. https://in.mathworks.com/help/matlab/ref/openproject.html
  4. https://in.mathworks.com/help/matlab/ref/matlab.project.project.removereference.html
  5. https://in.mathworks.com/help/simulink/slref/simulink.exporttotemplate.html
Hope this is helpful
  1 个评论
Eugene Ofori
Eugene Ofori 2023-9-29
编辑:Eugene Ofori 2023-11-9
Hi, thanks for the response. I saw the removeReference function but it doesn't seem to be what I was looking for. The function seems to alter the project and remove the references. The outcome I basically want and currently get through creating templates using the GUI, is when I create a new project from the template, it doesn't generate the external references but it still looks for them in the same relative path as the original project.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Create Large-Scale Model Components 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by