Site icon Real Technology Tools

MATLAB-Simulink: The Basic Requirements to Conquer Model-Based Design (1/2)

Designing Models

Let’s say you are done with modifying your model and you want to make sure you that it is “clean”  before closing it. In this article, we’ll see what “clean” means and how to actually make sure your model is clean.

What to Check

Here’s the list of the basic things we want to check so that we can say that the model is clean. We want to know whether or not:

Let’s break them down:

Check Automatically

The goal here is not to be careful about not making those mistake. This would be too much work and too time-consuming. The goal is to make a modification without any precaution and then to use a function to check automatically that no errors have been made so that you don’t even have to think about this. If the function doesn’t generate an error, you’re good to go. Here is the function that will identify if one of the three mistakes listed above has been made.

function checkModel(model)
 
% 1. make sure no libraries are disabled
disabledLibraries = find_system(model, 'StaticLinkStatus', 'inactive');
if ~isempty(disabledLibraries)
    error('XXXXXXXXXX-Error-XXXXXXXXXX At least one library is disabled.');
end
 
% 2. make sure the model has been saved
if strcmp(get_param(model,'Dirty'),'on')
    error(['XXXXXXXXXX-Error-XXXXXXXXXX ' model ' has been modified but not saved.']) 
end
 
% 3. clear and make sure that the model runs
evalin('base', 'clear all ; close all ; bdclose all ; clc') % clear the workspace
open(model)
try sim([model '.slx'])
catch 
    error('XXXXXXXXXX-Error-XXXXXXXXXX The model does not run.');
end
 
return

Note: this function has to be used when the model is already opened (i.e., it’s meant to be used after being done with modifying your model).

If you want to see how this works on a practical example, see this article: MATLAB: Model-Based Design Tools for MATLAB & Simulink (2/2)

Exit mobile version