mlock not working, variables getting cleared in function switch.
4 次查看(过去 30 天)
显示 更早的评论
M using Multiple function in a single '.m' file.
when i make a function call the previous data of the calling function is getting cleared.
Even if i use mlock then also data is getting cleared from in between function calls.
Is there any other method i can preserve variable and data of my main function.
2 个评论
Geoff Hayes
2020-8-25
as - how are you storing the previous data? Is this data the output from calling this function before? Can you provide an example that demonstrates this problem?
回答(1 个)
Jan
2020-8-25
编辑:Jan
2020-8-25
Create this file:
function demo(In)
persistent v
switch lower(In)
case 'lock'
mlock
disp('locked')
case 'unlock'
munlock
disp('unlocked')
case 'set'
v = rand
case 'show'
v
end
end
Now run it:
demo('show') % v is []
demo('set')
clear('demo') % v is cleared
demo('show') % v is []
demo('set') % v is set to a value
demo('show') % v keeps its value
demo('lock')
clear('demo') % v is not cleared
demo('show') % v kept its value
demo('unlock')
clear('demo') % v is cleared
demo('show') % v is [] again
So usually declaring a variable as persistent is sufficient already and you need mlock only, if a brute program calls clear all (which is a bad programming pattern!).
2 个评论
Jan
2020-8-30
Of course the workspaces are cleared,when a function is left, except if a variable is stored persistently. So just import the "ROM file" (what ever this means) into a variable, which is declared as persistent.
另请参阅
类别
在 Help Center 和 File 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!