forked from MouseLightProject/carver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_or_read_from_memo.m
21 lines (21 loc) · 994 Bytes
/
compute_or_read_from_memo.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function result = compute_or_read_from_memo(cache_folder_path, ...
memo_base_name, ...
compute_function)
% A function to help with the common pattern of wanting to cache expensive-to-compute quantities on disk
memo_file_name = horzcat(memo_base_name, '.mat') ;
memo_file_path = fullfile(cache_folder_path, memo_file_name) ;
if exist(cache_folder_path, 'file') ,
if exist(memo_file_path, 'file') ,
load(memo_file_path, 'result') ;
else
result = feval(compute_function) ;
save(memo_file_path, '-mat', '-v7.3', 'result') ;
end
else
mkdir(cache_folder_path) ;
% We know memo_file_name doesn't exist, b/c we just created its
% parent folder
result = feval(compute_function) ;
save(memo_file_path, '-mat', '-v7.3', 'result') ;
end
end