MATLAB For Beginners: 20-Minute Video Training Course. (Grab The Training Course and The Cheat Sheet for FREE)
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:
- The libraries are linked.
- The model is saved.
- The model can run after having cleaned our workspace.
Let’s break them down:
- The model is saved. This check will make sure that you can close the model (which is necessary to run the last check).
- The libraries are linked. If the libraries are not linked, this means that there is a chance that the library block in the library and the library block in your model are not the same. If you are not careful and modify one them, you only have a 50% chance of modifying the more updated version.
- The model can run after having cleaned your workspace.
- The model can run. If you start to modify a model and, you see that after you modified it, your model doesn’t run, that means you’ve made a mistake, right? Wrong! It means you’ve made a mistake if and only if the model could run prior to you making the modification. This is why it’s crucial to make sure the model can run at all times. Otherwise, you’d end up correcting modifications that have been made in the past, which is particularly annoying, especially if you’re not the one who made the modification causing the error.
- After having cleaned your workspace. If your modifications involve defining new parameters, it can be easy to define them in your workspace and forget to define them in the file that initializes your model when you open it. Cleaning everything and trying to run your model again will prevent you from making that mistake.
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)