Constant, Persistent value, and profile versioning

1 visualización (últimos 30 días)
Marco
Marco el 29 de Mzo. de 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

Respuesta aceptada

Jacob Halbrooks
Jacob Halbrooks el 29 de Mzo. de 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 comentario
Marco
Marco el 29 de Mzo. de 2012
thank you so much, i'll definitely go for the second solution: so simple, so elegant.
thanks again!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Scope Variables and Generate Names en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by