How not to reload persistent variables twice?

2 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Standard File Formats 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by