-
Notifications
You must be signed in to change notification settings - Fork 0
/
autosave.asv
151 lines (129 loc) · 4.67 KB
/
autosave.asv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
% [] = autosave(ratname, varargin)
%
% Loosely moeled on save_solouiparamvalues; this will
% 1) obtain or create a session_id stored in global autosave_session_id
% 2) use user-specified animal name to, if needbe make a directory
% 3) in that directory, it will
% a) create a file {animalid}_{sessionid}.mat, saving ALL trials to it
% b) OR copy above file to {animalid}_{sessionid}_last.mat and then a)
%
%
% PARAMETERS:
% ----------
%
% ratname This will determine which directory the file goes into.
%
% OPTIONAL PARAMETERS:
% --------------------
%
% child_protocol by default, empty. If non-empty, should be an SPH
% that holds an object whose class will indicate the
% class of the child protocol who is the real
% owner of the vars to be saved.
%
% asv by default, zero. If 1, this is an non-interactive
% autosave, and the file will end in _ASV.mat. If 0,
% this is a normal file.
%
% interactive by default 1; dialogues with the user to determine
% the file in which the data will be saved. If 0,
% the default suggested filename is used, with
% possible overwriting and no questions asked.
%
% commit by default, 0; if 1, tries to add and commit to CVS
% the recently saved file.
%
% remove_asvs by default, 1. If remove_asvs==1 AND asv ~= 1
% (i.e., this NOT an asv save), then
% after doing the regular saving of the data, any
% ASV file of the same date will be removed (i.e.,
% cleanup).
%
% EXAMPLE CALL:
% -------------
%
% >> save_solouiparamvalues(ratname, 'commit', 1);
%
function [] = autosave(ratname, varargin)
pairs = { ...
'child_protocol', [] ; ...
'asv', 0; ...
'interactive' 1 ; ...
'commit' 0 ; ...
'remove_asvs' 1 ; ...
};
parse_knownargs(varargin, pairs);
if isempty(child_protocol),
owner = determine_owner;
else
owner = class(value(child_protocol)); % the child protocol owns all vars
% owner = c(2:end);
end;
% Create session ID
if (length(whos('global','autosave_session_id')) == 0)
global autosave_session_id;
autosave_session_id = datestr(now,30);
else
global autosave_session_id;
end
% Did I mention I hate most solo code because there is almost NO comments?
% --- prelims -- access workspace, figure datapath otu
owner = owner;
global Solo_datadir;
if isempty(Solo_datadir),
Solo_datadir=[pwd filesep '..' filesep 'SoloData'];
end;
data_path = [Solo_datadir filesep 'Data'];
if ~exist(data_path, 'dir'),
success = mkdir(Solo_datadir, 'Data');
if ~success, error(['Couldn''t make directory ' data_path]); end;
end;
handles = get_sphandle('owner', owner);
k = zeros(size(handles));
for i=1:length(handles),
if get_saveable(handles{i}), k(i) = 1; end;
end;
handles = handles(find(k));
% --- start building the written arrays
saved = struct; saved_history = struct; saved_autoset = struct;
for i=1:length(handles),
saved.(get_fullname(handles{i})) = value(handles{i});
saved_history.(get_fullname(handles{i}))= get_history(handles{i});
saved_autoset.(get_fullname(handles{i}))= get_autoset_string(handles{i});
end;
protocol_name = get_sphandle('owner', owner, 'name', 'protocol_name');
if ~isempty(protocol_name),
protocol_name = protocol_name{1};
fig_position = get(...
findobj(get(0, 'Children'), 'Name', value(protocol_name)),'Position');
else
fig_position = [];
end;
% Now set the path for the data file
if data_path(end)~=filesep, data_path=[data_path filesep]; end;
rat_dir = [data_path ratname];
if ~exist(rat_dir)
success = mkdir(data_path, ratname);
if ~success, error(['Couldn''t make directory ' rat_dir]); end;
end;
if rat_dir(end)~=filesep, rat_dir=[rat_dir filesep]; end;
pname = rat_dir;
fname = [ 'data_' owner '_' ratname '_' ...
yearmonthday '_ASV.mat'];
% --- copy to _last if needbe
if (exist([pname fname], 'file') ~= 0)
end
% --- The WRITE:
save([pname fname], 'saved', 'saved_history', 'saved_autoset', ...
'fig_position');
% Make sure it is a .mat extension:
[path, name, ext] = fileparts([pname fname]);
if ~strcmp('.mat', ext), fname = [name '.mat']; end;
if ~asv,
asv_pname = rat_dir;
asv_fname = [ 'data_' owner '_' ratname '_' ...
yearmonthday '_ASV.mat'];
if exist([asv_pname asv_fname], 'file'),
delete([asv_pname asv_fname]);
end;
end;