-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaveas_csv.m
50 lines (48 loc) · 1.98 KB
/
saveas_csv.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
function saveas_csv( filename, Y, X, yaxisscale )
%SAVEAS_CSV saves the data Y as a column vector in text form to
% FILENAME.csv
%
% Arguments:
% FILENAME filename without extension. The extension is added.
% Y y data vector to be saved in the second column of the
% text output file. The data is stored as floating
% point with a precision of 5 decimals.
%
% Optional arguments:
% X x data vector, must have the same length as Y.
% (Default: index of Y)
% Integer values are saved without decimals, floating
% point numbers with a precision of 5 decimals.
% YAXISSCALE type of scaling for the Y axis. Use 'linear'
% (Default) to leave Y data unchanged and the file is
% written with the extension '.csv'. With 'log' the
% logarithm of the Y data is stored and the file
% extension is '.log.csv'.
%
% Copyright 2021, C. Minz. BSD 3-Clause License.
if nargin < 4
yaxisscale = 'linear';
end
if strcmp( yaxisscale, 'linear' )
% save to data file, y-axis in linear scale:
fileID = fopen( sprintf( '%s.%s', filename, 'csv' ), 'w' );
for i = 1 : length( Y )
if nargin < 3
fprintf( fileID, '%d, %0.5f\n', i, Y( i ) );
elseif isinteger( X( i ) )
fprintf( fileID, '%d, %0.5f\n', X( i ), Y( i ) );
else
fprintf( fileID, '%0.5f, %0.5f\n', X( i ), Y( i ) );
end
end
fclose( fileID );
elseif strcmp( yaxisscale, 'log' )
% save to data file, y-axis in linear scale:
Y = log( Y );
if nargin < 3
saveas_csv( sprintf( '%s.%s', filename, 'log' ), Y );
else
saveas_csv( sprintf( '%s.%s', filename, 'log' ), Y, X );
end
end
end