How not to reload persistent variables twice?

2 ビュー (過去 30 日間)
Mr M.
Mr M. 2017 年 7 月 21 日
編集済み: Jan 2017 年 7 月 21 日
I wnat to use persistent variables to speed up, so I want to genrate data, and make it persistent at the first call, but how to check next time, that this is not the first call, and I dont want to genrate the data again?

採用された回答

Walter Roberson
Walter Roberson 2017 年 7 月 21 日
if isempty(VariableName)
... calculate initial value for variable
end

その他の回答 (1 件)

Jan
Jan 2017 年 7 月 21 日
編集済み: Jan 2017 年 7 月 21 日
This is explained in the documentation: doc persistent:
function arrayToXLS(A, xlsfile, x1, x2)
persistent dblArray;
if isempty(dblArray)
disp 'Writing spreadsheet file ...'
xlswrite(xlsfile, A);
end
Undefined persistent variables are empty. If your real data might be empty also, use a persistent flag in addition:
persistent Data initialized
if isempty(initialized)
initialized = true;
Data = <what eve you want, even empty array>
end

カテゴリ

Help Center および File ExchangeStandard File Formats についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by