Getting undefined model variables

9 Ansichten (letzte 30 Tage)
Adam
Adam am 9 Apr. 2011
Hello, I would like to ask if there is a proper way to get information about Simulink model's variables that are not set in the workspace. I mean before I run it and get an error message. Thank you, Adam
  2 Kommentare
Arnaud Miege
Arnaud Miege am 11 Apr. 2011
I don't understand your question, can you clarify and elaborate?
Walter Roberson
Walter Roberson am 11 Apr. 2011
My personal interpretation was,
Is there a way to analyze a model before running it, to determine the model variables that should be initialized from the workspace, but for which the workspace variable has not been initialized?
(I can see how such a thing could be useful, so you don't waste your time running through a number of steps only to have the program fall over because you forgot to initialize something. On the other hand, my training in computing theory suggests to me that the answer is "You cannot _reliably_ do this."

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Doug Eastman
Doug Eastman am 11 Apr. 2011
Here's a function that will find all undefined variables in a given model. It returns a cell array with the variable names and also creates all the variables (with a value of 0) in the base workspace. You can delete them all by running
clear(undefinedVars{:})
in the MATLAB workspace, where undefinedVars is the name of the return argument.
function undefinedVars = findUndefinedVars(modelName)
undefinedVars = [];
stillErrors = 1;
while stillErrors
try
Simulink.findVars(modelName);
stillErrors = 0;
catch err
% If the error is an undefined block parameter then find the name and add it to the list
cause = err.cause;
if ~isempty(cause) && strcmp(cause{1}.identifier,'Simulink:Parameters:BlkParamUndefined')
varName = regexp(cause{1}.message,'variable ''(\w+)''','tokens');
if ~isempty(varName)
undefinedVars = [undefinedVars varName{1}];
evalin('base',[varName{1}{1},'=0;']);
end
% Display any other errors as usual.
else
stillErrors = 0;
rethrow(err);
end
end
end
  2 Kommentare
Walter Roberson
Walter Roberson am 11 Apr. 2011
Nice work, Doug!
Adam
Adam am 11 Apr. 2011
Thank you Dough, this seems to work fine! And since there's probably no better way than catching errors (from model itself or findVars function), it's the best choice.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Arnaud Miege
Arnaud Miege am 11 Apr. 2011
If Walter's interpretation is correct, you can maybe use Simulink.findVars, but I think that probably requires the variables to be defined in the first place. Also note the limitations as to what it can and can't find. That's been available since R2010a.
HTH,
Arnaud
  2 Kommentare
MarkB
MarkB am 11 Apr. 2011
I don't think that "Simulink.findVars" will work in this case because I'm pretty sure that it requires that the model be able to update. Unfortunately, without the data in the workspace, it might not update.
Adam
Adam am 11 Apr. 2011
Walter's interpretation is right, but as Mark mentioned, "Simulink.findVars" doesn't work. That was my first try.

Melden Sie sich an, um zu kommentieren.


Rob Graessle
Rob Graessle am 11 Apr. 2011
You could place something like this in the InitFcn callback of your model:
if ~exist('a', 'var') % If 'a' is not initialized
a=3; % Initialize it...
disp('Variable a not initialized!'); % ...or display an error
end
This would only work for variables in the MATLAB base workspace though, not the model workspace.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by