Commit 2098beb0 authored by Stéphane Adjemian's avatar Stéphane Adjemian
Browse files

Added save method (m, mat, csv formats) in dynSeries class.

parent 73c96097
function save(A,basename,format)
% Saves a dynSeries object on disk.
if nargin<3 || isempty(format)
format = 'csv';
end
if nargin<2 || isempty(basename)
basename = inputname(1);
end
switch format
case 'm'
fid = fopen([basename, '.m'],'w');
fprintf(fid,'%% File created on %s.\n',datestr(now));
fprintf(fid,'\n');
fprintf(fid,'FREQ__ = %s;\n',num2str(A.freq));
fprintf(fid,'INIT__ = '' %s'';\n',A.init.format);
fprintf(fid,'\n');
for v=1:A.vobs
fprintf(fid,'%s = [\n', A.name{v});
fprintf(fid,'%15.8g\n',A.data(1:end-1,v));
fprintf(fid,'%15.8g];\n\n',A.data(end,v));
end
fclose(fid);
case 'mat'
FREQ__ = A.freq;
INIT__ = A.init.format;
str = [];
for v=1:A.vobs
str = [str, A.name{v} ' = A.data(:,' num2str(v) ');' ];
end
eval(str);
save([basename '.mat'],'INIT__','FREQ__',A.name{:});
case 'csv'
fid = fopen([basename, '.csv'],'w');
fprintf(fid,', %s', A.name{:});
fprintf(fid,'\n');
for t=1:A.nobs
date = A.init+(t-1);
str = sprintf(', %15.8g',A.data(t,:));
fprintf(fid, '%s%s\n',date.format,str);
end
fclose(fid);
end
%@test:1
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$
%$ % Instantiate a time series object.
%$ try
%$ ts1 = dynSeries(A,[],A_name,[]);
%$ save(ts1,[],'csv');
%$ t = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ T = all(t);
%@eof:1
%@test:2
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$
%$ % Instantiate a time series object.
%$ try
%$ ts1 = dynSeries(A,[],A_name,[]);
%$ save(ts1,[],'m');
%$ t = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ T = all(t);
%@eof:2
%@test:3
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$
%$ % Instantiate a time series object.
%$ try
%$ ts1 = dynSeries(A,[],A_name,[]);
%$ save(ts1,[],'m');
%$ t = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ T = all(t);
%@eof:3
......@@ -69,6 +69,9 @@ if length(S)==1 && isequal(S.type,'.')
us = builtin('subsref', ts, S);
case {'log','exp'} % Give "dot access" to public methods.
us = feval(S.subs,ts);
case {'save'}
us = NaN;
save(ts);
otherwise % Extract a sub-object by selecting one variable.
ndx = strmatch(S.subs,ts.name);
if ~isempty(ndx)
......@@ -119,9 +122,17 @@ end
if (length(S)==1) && isequal(S(1).type,'{}')
us = extract(ts,S(1).subs{:});
return
end
if (length(S)==2) && isequal(S(1).subs,'save') && isequal(S(1).type,'.') && isequal(S(2).type,'()')
us = NaN;
save(ts,S(2).subs{:});
return
end
%@test:1
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
......@@ -306,3 +317,40 @@ end
%$ T = all(t);
%@eof:6
%@test:7
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$
%$ % Instantiate a time series object.
%$ try
%$ ts1 = dynSeries(A,[],A_name,[]);
%$ ts1.save;
%$ t = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ T = all(t);
%@eof:7
%@test:8
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
%$
%$ % Define names
%$ A_name = {'A1';'A2'};
%$
%$ % Instantiate a time series object.
%$ try
%$ ts1 = dynSeries(A,[],A_name,[]);
%$ ts1.save('test_generated_data_file','m');
%$ t = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ T = all(t);
%@eof:8
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment