Constant, Persistent value, and profile versioning

1 次查看(过去 30 天)
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

采纳的回答

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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Scope Variables and Generate Names 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by