필터 지우기
필터 지우기

Constant, Persistent value, and profile versioning

조회 수: 2 (최근 30일)
Marco
Marco 2012년 3월 29일
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