Constant, Persistent value, and profile versioning

1 view (last 30 days)
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

Accepted Answer

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

Sign in to comment.

More Answers (0)

Categories

Find more on Scope Variables and Generate Names in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!