Clear Filters
Clear Filters

How to save and update a simulink model via code?

4 views (last 30 days)
I am creating a function that checks simulink models for errors,reports them, and corrects them. I would like for the program to also be able to update and save the model once it has completed it tasks, How would I do that?
For example in this function the objective is to check testpoints lines and make sure the log data box is checked and the correct class is used. Once that is done I want it to update and save the model.
function testPointCheck( app )
%%%%%%App Specific Stuff
% stop the timer while we are running
stop(app.projectTimer);
% disable the generate button while we are running
%app.RunCleanupButton.Enable = 'off';
app.StatusArea.Value = 'Running test point check function. This can take awhile...';
filename = app.TopModelEditField.Value;
[folder, system, extension] = fileparts(filename);
load_system(system);
%%%%%%End App Specific Stuff
%Remove Old whitespace findings mat file
projects = slproject.getCurrentProjects;
projectRoot = projects.RootFolder;
saveFolder = [projectRoot '\scripts'];
saveFileFullName = [saveFolder '\testPoint.mat'];
warning('off','all')
delete (saveFileFullName);
%delete ('ErrorReport.xlsx');
%xlswrite('ErrorReport.xlsx','ErrorReport');
warning('on','all')
%allLines=find_system(system,'FindAll','on','type','line');
allLines = recursiveModelReferenceLines(char(getfullname(system)),[]);
testPointLines = [];
names = [];
%{
In this section the program is searching for lines with the test point
Box checked, stores them in a separate variable and retrieves their
name. After that the program corrects the errors
%}
for i=1:length(allLines)
point = get(allLines(i),'TestPoint');
if point ==1
testPointLines = vertcat(testPointLines,allLines(i));
%names =vertcat(names,get(testPointLines,'Name'));
cellarray =[get_param(testPointLines,'Parent') get(testPointLines,'Name')];
names = cellarray(:,2);
end
end
if (~isempty(names))
save(saveFileFullName,'testPointLines')
varName = {'Model','Line'};
for i=1:length(testPointLines)
unLoggedLines = get(testPointLines(i),'DataLogging');
Class = get(testPointLines(i),'StorageClass');
isClassGood = strcmp(Class, 'ExportedGlobal');
if unLoggedLines ==0
T = table(cellarray(:,1),cellarray(:,2),'VariableNames',varName);
writetable(T,'ErrorReport.xlsx','Sheet','unLoggedLines');
set(testPointLines(i),'DataLogging',1)
end
if isClassGood ==0
S = table(cellarray(:,1),cellarray(:,2),'VariableNames',varName);
writetable(S,'ErrorReport.xlsx','Sheet','incorrectStorageClass');
set(testPointLines(i),'StorageClass','ExportedGlobal')
end
end
set_param(gcs,'SimulationCommand','Update')
end
save_system(system);
end
  2 Comments
Ameer Hamza
Ameer Hamza on 27 Apr 2020
You already have the lines
set_param(gcs,'SimulationCommand','Update');
save_system(system);
Are you getting any error?
Deon Hargrove
Deon Hargrove on 27 Apr 2020
Edited: Deon Hargrove on 27 Apr 2020
No I am not, but I still have to manually save and update the model. For some reason the commands are not be activated or I am not using them correctly.
Update: I am not geeting this error
The specified top model was not saved due to the existence of dirty referenced models (for example,
'lm6Core/parameters'). Use the save_system name-value pair 'SaveDirtyReferencedModels','on' to
save the specified top model as well as all the dirty referenced models.

Sign in to comment.

Answers (0)

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!