-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafe_saveall.m
56 lines (51 loc) · 2.21 KB
/
safe_saveall.m
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
function safe_saveall(filename, newData)
% function SAFE_SAVEALL is a custom replacement for MATLAB's save function
% to ensure that data does not get saved unexpectedly.
% INPUTS:
% filename: input string with filename
% newData: input data that needs to be saved
% Check if file already exists
if isfile(filename)
% Load the existing data
if contains(filename, '.mat')
oldData = importdata(filename);
elseif contains(filename, '.csv') || contains(filename, '.txt') || contains(filename, '.xlsx')
oldData = readtable(filename);
else
error('Unsupported file type.');
end
% Compare the new data with the old data
if isa(newData,"double")
equality_check = isequaln(round(newData,10),round(oldData,10));
elseif strcmp(string(class(newData)),"LinearModel") == 1
equality_check = compare_LinearModels(newData,oldData);
else
equality_check = isequaln(newData,oldData);
end
if equality_check == 1
disp('Data is consistent. No need to save.');
else
% For different data, create a new filename
[path, name, ext] = fileparts(filename);
timestamp = datestr(now, 'yymmdd');
newFilename = strcat(name, "_", timestamp, ext);
% Save with new filename
if contains(ext, '.mat')
save(fullfile(path, newFilename), 'newData');
elseif contains(ext, '.csv') || contains(ext, '.txt') || contains(ext, '.xlsx')
writetable(newData, fullfile(path, newFilename));
end
disp(['Data is different. Saved with a new filename: ' newFilename]);
end
else
% Save the new data
if contains(filename, '.mat')
save(filename, 'newData');
elseif contains(filename, '.csv') || contains(filename, '.txt') || contains(filename, '.xlsx')
writetable(newData, filename);
else
error('Unsupported file type.');
end
disp(['File does not exist. Saved data to: ' filename]);
end
end