'Storing' variables in RAM

10 ビュー (過去 30 日間)
Daniel
Daniel 2021 年 5 月 7 日
コメント済み: Daniel 2021 年 5 月 10 日
Is there a way to store data without writing it to the drive?
Result should be that 'clear all' does not affect the variable/handle, but EEPROM doesn't constantly have to be written to. Similar to the Instrument Toolbox, where instruments are kept regardless of what happens in the workspace and are only removed with the 'delete'-command.
And yes, i know, 'Don't use clear all, it's resource intensive and almost never necessary'. Not an option.

採用された回答

Jan
Jan 2021 年 5 月 7 日
編集済み: Jan 2021 年 5 月 7 日
Store the wanted data persistently:
function Out = DataVault(Cmd, Name, Data)
persistent Stored
mlock;
switch lower(Cmd)
case 'store'
Stored.(Name) = Data;
case 'get'
Out = Stored.(Name);
case 'clear'
Stored = [];
munlock;
otherwise
error('Jan:DataValut:BadCmd', 'Unknown command: [%s]', Cmd);
end
Now call it:
ValuableData = 4711;
DataVault('store', 'Box1', ValuableData);
clear all
ValuableData = DataValut('get', 'Box1')
Of course, clean code would simply omit all clear all calls, because they remove all (unlocked) functions from the memory. You say, that this is not an option. I resist on preferring trustworthy code, which does not contain junk.
If the clear all comes from evil subfunctions, you can either shadow the clear command by defining your own clear function, or if they are scripts, define a variable "all", such that "clear all" clears this variable only. Brrr.
  1 件のコメント
Daniel
Daniel 2021 年 5 月 10 日
Evil, evil... Not keen on a coding war, so I'm probably going to let the 'define all as a variable'-approach pass, but if I implemented a clear function in my library that clears everything but what I want to keep, that might just work. Thanks for the advice!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by