Constant, Persistent value, and profile versioning

1 vue (au cours des 30 derniers jours)
Marco
Marco le 29 Mar 2012
Hi, i created a matlab environment based on profiles. to each profile correspond a set of libraries with their version.
when launching matlab the user is prompted with the choice of the desired profile and, after his decision, the correct path is loaded.
i need the information on the chosen profile to be constantly stored in memory so that no clear will delete it.
I saw that constants are defined through classes. My problem is that the value of this constant is not known apriori but depends on a user choice. so i can't create properties with such value/values.
on the other side defining methods to set the values would imply the creation of an instance of the class (thing that would go against my requirement of persistency after a clear command)
Does anybody has a solution to suggest to help me out?
Thanks.
Nene

Réponse acceptée

Jacob Halbrooks
Jacob Halbrooks le 29 Mar 2012
One option is to use a CONSTANT property assigned to an object instance. See the doc Setting Constant Property Default section for an example of this that protects against clearing.
Another approach is to use MLOCK in a function with a PERSISTENT variable to prevent the variable from being cleared. For example, create a function like this:
function outPref = myPref(inPref)
mlock;
persistent privatePref;
if isempty(privatePref)
privatePref = 'default';
end
if nargin
privatePref = inPref;
end
if nargout
outPref = privatePref;
end
Now you can store a value in there that persists through clear:
>> q=myPref
q =
default
>> myPref('new')
>> clear all
>> q=myPref
q =
new
  1 commentaire
Marco
Marco le 29 Mar 2012
thank you so much, i'll definitely go for the second solution: so simple, so elegant.
thanks again!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Scope Variables and Generate Names dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by