-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatax.m
135 lines (128 loc) · 3.58 KB
/
datax.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
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
function datax(varargin)
% DATAX print variables to file (for use with `datax.sty` in LaTeX).
% DATAX ([filename], variables)
% If the first argument is a string, it's the filename. Defaults to "data.tex"
% variables is a number of arguments, either
% variable, unit, format
% variable, unit
% variable, format *or*
% variable
% Calls that break with this order will simply lead to strange data files.
% If a string argument contains '%', it's assumed to be format.
% The default format is "%.4g"
% unit should be given as siunitx strings: '\meter\per\second' etc
% Example:
% a = 24.35;
% b = 2;
% c = 7.44;
% d = 12;
% datax('data.tex',a,'\meter','%.3g',b,'\kilo\gram',c,'%.2g',d)
% % prints data corresponding to
% % a = 24.4 \meter
% % b = 2 \kilo\gram
% % c = 7.4
% % d = 12
argn = 1;
if isstring(varargin{1}) || ischar(varargin{1})
filename=varargin{1};
argn=argn+1;
else
filename="data.tex";
end
f = fopen(filename,'w');
fprintf(f,"%% File auto-generated by LaTeXDatax.m. Will be overwritten.\n");
while argn<=nargin
value = varargin{argn};
origargn = argn;
argn = argn+1;
if argn<=nargin && (isstring(varargin{argn})||ischar(varargin{argn}))
if contains(varargin{argn},'%')
unit = "";
format = varargin{argn};
argn = argn+1;
else
unit = varargin{argn};
argn = argn+1;
if argn<=nargin && (isstring(varargin{argn})||ischar(varargin{argn})) && contains(varargin{argn},'%')
format = varargin{argn};
argn = argn+1;
else
format = "";
end
end
else
unit="";
format="";
end
printdatum(f,inputname(origargn),value,unit,format);
end
fclose(f);
end
function printdatum(f,tag,value,unit,format)
% PRINTDATUM print variable named `tag` with given value, unit and format to iostream f
% PRINTDATUM ( f , tag , value , unit , format )
%
% See also DATAX.
fprintf(f,"\\pgfkeyssetvalue{/datax/%s}{",tag);
if format == ""
format = "%.4g";
end
if unit == ""
if isstring(value)
fprintf(f,"%s",value);
return
end
fprintf(f,strcat("\\num{",format,"}}\n"),value);
return
end
fprintf(f,strcat("\\qty{",format,"}{%s}}\n"),value,unit);
end
%{
% Archive {{{
% Archived, might still have a use in the future...
function datax(filename, tags, values, units, formats)
% DATAX ( filename, tags, values, units, formats ) save the given data to file
% FILENAME, for use with the datax.sty LaTeX package. UNITS is a single unit
% string or a vector of unit strings. These should be siunitx-style strings
% ("\\kilo\\gram\\per\\meter\\tothe2"). If left out, or explicitly set to "",
% the number is printed without a unit.
% FORMATS is a single format string or a vector of c-style format strings for
% the values. If this is left out, or explicitly given as "", the default of
% "%.4g" is used.
if nargin<4
units = strings(size(tags));
end
if nargin<5
formats = strings(size(tags));
end
if numel(values) ~= numel(tags)
if numel(values) == 1
values = repmat(values,size(tags));
else
error("Must have equal numbers of values and tags");
end
end
if numel(units) ~= numel(tags)
if numel(units) == 1
units = repmat(units,size(tags));
else
error("Must have equal numbers of units and tags");
end
end
if numel(formats) ~= numel(tags)
if numel(formats) == 1
formats = repmat(formats,size(tags));
else
error("Must have equal numbers of formats and tags");
end
end
f = fopen(filename,'w');
for i = 1:length(tags)
fprintf(f,"\\pgfkeyssetvalue{%s}{",tags(i));
printdatavalue(f,values(i),units(i),formats(i));
fprintf(f,"}\n");
end
fclose(f);
end
% Archive }}}
%}