-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintMatStructure.m
More file actions
61 lines (52 loc) · 1.66 KB
/
PrintMatStructure.m
File metadata and controls
61 lines (52 loc) · 1.66 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
function PrintMatStructure(matFile, outputFile)
% PrintMatStructure - 将 MAT 文件的结构打印到文本文件
%
% 用法:
% PrintMatStructure % 弹出文件选择对话框
% PrintMatStructure('result.mat')
% PrintMatStructure('result.mat', 'structure.txt')
if nargin < 1 || isempty(matFile)
[file, path] = uigetfile('*.mat', '选择 MAT 文件');
if isequal(file, 0), return; end
matFile = fullfile(path, file);
end
if nargin < 2 || isempty(outputFile)
[folder, name, ~] = fileparts(matFile);
outputFile = fullfile(folder, [name '_structure.txt']);
end
% 使用 whos 获取变量信息
info = whos('-file', matFile);
data = load(matFile);
% 写入文件
fid = fopen(outputFile, 'w', 'n', 'UTF-8');
fprintf(fid, '# %s\n# %s\n\n', matFile, datetime('now'));
for i = 1:length(info)
fprintf(fid, '%s: [%s] %s\n', info(i).name, info(i).class, sizeStr(info(i).size));
if isstruct(data.(info(i).name)) && isscalar(data.(info(i).name))
printStruct(fid, data.(info(i).name), ' ');
end
end
fclose(fid);
fprintf('已保存: %s\n', outputFile);
end
function printStruct(fid, s, indent)
fields = fieldnames(s);
for i = 1:length(fields)
v = s.(fields{i});
if isstruct(v) && isscalar(v)
fprintf(fid, '%s.%s: [struct]\n', indent, fields{i});
printStruct(fid, v, [indent ' ']);
else
fprintf(fid, '%s.%s: [%s] %s\n', indent, fields{i}, typeStr(v), sizeStr(size(v)));
end
end
end
function s = typeStr(v)
s = class(v);
if isnumeric(v) && ~isreal(v)
s = [s ' complex'];
end
end
function s = sizeStr(sz)
s = strjoin(string(sz), 'x');
end