-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathSaveData2File.m
More file actions
127 lines (100 loc) · 2.79 KB
/
SaveData2File.m
File metadata and controls
127 lines (100 loc) · 2.79 KB
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
function [Status, Message] = SaveData2File(Data, FileName, ColNamesCell,varargin)
%% SaveData2File
% by LiYang_faruto
% Email:farutoliyang@foxmail.com
% 2015/05/01
%% 输入输出预处理
Status = 1;
Message = [];
if nargin < 3 || isempty(ColNamesCell)
ColNamesCell = [];
end
if nargin < 2 || isempty(FileName)
FileName = 'OutData.xlsx';
end
if nargin < 1 || isempty(Data)
Status = 0;
Message = '缺少输入参数,请输入待保存的数据!';
disp(Message);
return;
end
% ColNamesCell 处理
[Rlen, Clen] = size(Data);
if ~isempty(ColNamesCell)
tlen = length(ColNamesCell);
if tlen < Clen
for i = tlen+1:Clen
str = ['VarName',num2str(i)];
ColNamesCell{i} = str;
end
end
if tlen > Clen
ColNamesCell = ColNamesCell(1:Clen);
end
end
% FileName 检查处理
ind = find(FileName == '.', 1,'last');
if isempty(ind)
FileName = [FileName,'.xlsx'];
ind = find(FileName == '.', 1,'last');
end
ExtCell = {'.txt','.dat','.csv','.xls','.xlsb','.xlsx','.xlsm'};
ExtName = FileName(ind:end);
if ~ismember(ExtName, ExtCell)
Status = 0;
Message = '请检查输入的文件扩展名!(仅支持如下扩展名)';
disp(Message);
disp(ExtCell);
return;
end
tExtCell = {'.xls','.xlsb','.xlsx','.xlsm'};
if ismember(ExtName, tExtCell)
warning('off')
end
%% Main
switch class( Data )
case 'double'
[tS, tM] = Double2File(Data, FileName, ColNamesCell,varargin{:});
case 'cell'
[tS, tM] = Cell2File(Data, FileName, ColNamesCell,varargin{:});
case 'struct'
[tS, tM] = Struct2File(Data, FileName, ColNamesCell,varargin{:});
otherwise
Status = 0;
Message = '输入数据类型未知!请检查!';
disp(Message);
return;
end
end
%% sub fun-Double2File
function [tS, tM] = Double2File(Data, FileName, ColNamesCell,varargin)
tS = 1;
tM = [];
tCell = num2cell(Data);
if ~isempty( ColNamesCell )
tCell = [ColNamesCell;tCell];
end
Fun = @(x)( num2str(x) );
tCell = cellfun( Fun,tCell, 'UniformOutput', false);
T = cell2table(tCell);
writetable(T,FileName,'WriteVariableNames',false,varargin{:});
end
%% sub fun-Cell2File
function [tS, tM] = Cell2File(Data, FileName, ColNamesCell,varargin)
tS = 1;
tM = [];
tCell = Data;
if ~isempty( ColNamesCell )
tCell = [ColNamesCell;tCell];
end
Fun = @(x)( num2str(x) );
tCell = cellfun( Fun,tCell, 'UniformOutput', false);
T = cell2table(tCell);
writetable(T,FileName,'WriteVariableNames',false,varargin{:});
end
%% sub fun-Struct2File
function [tS, tM] = Struct2File(Data, FileName, ColNamesCell,varargin)
tS = 1;
tM = [];
[tS, tM] = Cell2File(Data, FileName, ColNamesCell,varargin{:});
end