How to define a variable that store its value after closing MATLAB file?

3 ビュー (過去 30 日間)
aurora
aurora 2012 年 9 月 17 日
Hi,
I just want to declare a variable that holds its value through the whole file and even after closing it , also to initialize it externally . I tried to use 'persistent' variables but they're locally defined and so I have to initialize it locally , that makes its value initialized each time the function is called. When I define it global to avoid the repeated initialization through the same file , a warning appears 'the variable can apparently be used before it's defined' and an error 'undefined variable'. I need this variable to save txt files that contains the incremented variable value in its name.
Thanks in advance
  1 件のコメント
Jan
Jan 2012 年 9 月 18 日
編集済み: Jan 2012 年 9 月 18 日
Without the code the error message is meaningless when posted to the forum.

サインインしてコメントする。

採用された回答

aurora
aurora 2012 年 9 月 23 日
編集済み: aurora 2012 年 9 月 23 日
Thank you very much Jan Simon for your answer and thank u all
but I found a solution for my question , and it works as I want .. I just used a text file to keep the variables that I want them not to lose their values and I used them in my program (using save and load commands) only to be incremented to use them in naming a series of files . I don't know if I explained that well but I just want to close this question .

その他の回答 (2 件)

Nathaniel
Nathaniel 2012 年 9 月 17 日
If you're getting undefined variable errors for something you thought was supposed to be a global, then you haven't defined it to be global in the current context/scope.
function myfun
global param
if isempty(param) % hasn't been initialized
param = 1;
else % already initialized
param = param + 1;
end
% do something useful
So with that explained, why don't you simply pass in the value as an argument to the function? Using globals is generally frowned upon, since it can become difficult to keep track of which functions are using which globals and then when you start getting incorrect answers, it can take a lot of time to figure out why (if you're lucky enough to even notice that they're incorrect).
  1 件のコメント
aurora
aurora 2012 年 9 月 18 日
編集済み: aurora 2012 年 9 月 18 日
Thank you Nathaniel for your answer , yes the problem was that my global variables aren't initialized but I don't want to initialize them each time my .m file is implemented .
My problem solution is to store my variables in an external file and load it when I need them and also to save it when I need to hold their new values ..I did that and it works good for my purpose .

サインインしてコメントする。


Jan
Jan 2012 年 9 月 18 日
編集済み: Jan 2012 年 9 月 18 日
function out = myFunc(in)
persistent Data
if nargin == 0
Data = rand(10);
if nargout > 0
out = Data;
end
return;
end
... Your calculations come here
Now the persistent variable is initialized by:
myFunc;
And exported by:
data = myFunc;
And a standard call of the function is:
theOutput = myFunc(theInput);
I avoid working with GLOBALs strictly. They cause too much interferences and in larger programs it is the hell to find out, which subfunction had written the last changes.

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by