Skip to content
Snippets Groups Projects
Verified Commit 5436f91f authored by Stéphane Adjemian's avatar Stéphane Adjemian
Browse files

Fix compatibility issue with R2018b.

Methods or functions cannot be indexed using {} or . indexing.
parent 181bb997
No related branches found
No related tags found
No related merge requests found
Pipeline #11018 passed
......@@ -26,110 +26,125 @@ function ts = dseriesA2Q(ds, method)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
if ~isequal(ds.freq,1)
error('Conversion only allowed for annual data!')
end
dQ = dates(4, [ds.dates.year(1) 1]):dates(4, [ds.dates.year(end) 4]);
tdata = NaN(dQ.ndat, ds.vobs);
switch method
case 'equal-per-quarter'
counter = 1;
for i = 1:4:dQ.ndat
tdata(i:i+3,:) = repmat(ds.data(counter,:), [4,1]);
counter = counter+1;
if ~isequal(ds.freq,1)
error('Conversion only allowed for annual data!')
end
case 'cubic-spline'
for i = 1:length(ds.name)
dAO = firstobservedperiod(ds{ds.name{i}}).year(1):lastobservedperiod(ds{ds.name{i}}).year(1);
ids = find(dQ.year(:,1)==dAO(1),1);
ide = find(dQ.year(:,1)==dAO(end),1,'last');
qPoints = dAO(1):0.25:dAO(end);
tdata(ids+3:ide,i) = spline(dAO, ds{ds.name{i}}.data, qPoints); % First three datapoints are set to NaN
dQ = dates(4, [ds.dates.year(1) 1]):dates(4, [ds.dates.year(end) 4]);
tdata = NaN(dQ.ndat, ds.vobs);
switch method
case 'equal-per-quarter'
counter = 1;
for i = 1:4:dQ.ndat
tdata(i:i+3,:) = repmat(ds.data(counter,:), [4,1]);
counter = counter+1;
end
case 'cubic-spline'
for i = 1:length(ds.name)
dAO = get_periods();
ids = find(dQ.year(:,1)==dAO(1),1);
ide = find(dQ.year(:,1)==dAO(end),1,'last');
qPoints = dAO(1):0.25:dAO(end);
tdata(ids+3:ide,i) = spline(dAO, ds{ds.name{i}}.data, qPoints); % First three datapoints are set to NaN
end
case 'spline-sum'
for i = 1:length(ds.name)
dAO = get_periods();
ids = find(dQ.year(:,1)==dAO(1),1);
ide = find(dQ.year(:,1)==dAO(end),1,'last');
% Normalise the x point to start from 0
dAOTmp = dAO - (dAO(1));
dAO = [dAOTmp dAOTmp(end) + [1 2]];
qPoints = dAO(1):0.25:dAO(end);
% Create cumsum of the data with initial point set to 0
dI = [0; cumsum([ds{ds.name{i}}.data; ds{ds.name{i}}.data(end)])];
% Compute spline
dO = spline(dAO, dI, qPoints)';
% Compute first difference to get flow values
dOF = dO(2:end) - dO(1:end-1);
% Construct the final data
tdata(:,i) = dOF(ids:ide);
end
otherwise
error('Unknow method.')
end
case 'spline-sum'
for i = 1:length(ds.name)
dAO = firstobservedperiod(ds{ds.name{i}}).year(1):lastobservedperiod(ds{ds.name{i}}).year(1);
ids = find(dQ.year(:,1)==dAO(1),1);
ide = find(dQ.year(:,1)==dAO(end),1,'last');
% Normalise the x point to start from 0
dAOTmp = dAO - (dAO(1));
dAO = [dAOTmp dAOTmp(end) + [1 2]];
qPoints = dAO(1):0.25:dAO(end);
% Create cumsum of the data with initial point set to 0
dI = [0; cumsum([ds{ds.name{i}}.data; ds{ds.name{i}}.data(end)])];
% Compute spline
dO = spline(dAO, dI, qPoints)';
% Compute first difference to get flow values
dOF = dO(2:end) - dO(1:end-1);
% Construct the final data
tdata(:,i) = dOF(ids:ide);
ts = dseries(tdata, dQ, ds.name, ds.tex);
function dAO = get_periods()
t1 = firstobservedperiod(ds{ds.name{i}});
t2 = lastobservedperiod(ds{ds.name{i}});
dAO = t1.year(1):t2.year(1);
% TODO: Alternatively we could do the same in one line:
%
% dAO = firstobservedperiod(ds{ds.name{i}}).year(1):lastobservedperiod(ds{ds.name{i}}).year(1);
%
% but this does not work with R2018b. It works with R2021a, I could not test with R2019 and R2020.
% Should be tested if we decide to bump the Matlab version required.
end
otherwise
error('Unknow method.')
end
ts = dseries(tdata, dQ, ds.name, ds.tex);
return % --*-- Unit tests --*--
return % --*-- Unit tests --*--
%@test:1
ds = dseries(randn(10,2), '2000Y');
%@test:1
ds = dseries(randn(10,2), '2000Y');
try
ts = dseriesA2Q(ds, 'equal-per-quarter');
t(1) = true;
catch
t(1) = false;
end
try
ts = dseriesA2Q(ds, 'equal-per-quarter');
t(1) = true;
catch
t(1) = false;
end
if t(1)
t(2) = ts.vobs==2;
t(3) = ts.nobs==40;
t(4) = ts.dates(1)==dates('2000Q1');
end
if t(1)
t(2) = ts.vobs==2;
t(3) = ts.nobs==40;
t(4) = ts.dates(1)==dates('2000Q1');
end
T = all(t);
%@eof:1
T = all(t);
%@eof:1
%@test:2
ds = dseries(randn(10,2), '2000Y');
%@test:2
ds = dseries(randn(10,2), '2000Y');
try
ts = dseriesA2Q(ds, 'cubic-spline');
t(1) = true;
catch
t(1) = false;
end
try
ts = dseriesA2Q(ds, 'cubic-spline');
t(1) = true;
catch
t(1) = false;
end
if t(1)
t(2) = ts.vobs==2;
t(3) = ts.nobs==40;
t(4) = ts.dates(1)==dates('2000Q1');
end
if t(1)
t(2) = ts.vobs==2;
t(3) = ts.nobs==40;
t(4) = ts.dates(1)==dates('2000Q1');
end
T = all(t);
%@eof:2
T = all(t);
%@eof:2
%@test:3
ds = dseries(randn(10,2), '2000Y');
%@test:3
ds = dseries(randn(10,2), '2000Y');
try
ts = dseriesA2Q(ds, 'spline-sum');
t(1) = true;
catch
t(1) = false;
end
try
ts = dseriesA2Q(ds, 'spline-sum');
t(1) = true;
catch
t(1) = false;
end
if t(1)
t(2) = ts.vobs==2;
t(3) = ts.nobs==40;
t(4) = ts.dates(1)==dates('2000Q1');
end
if t(1)
t(2) = ts.vobs==2;
t(3) = ts.nobs==40;
t(4) = ts.dates(1)==dates('2000Q1');
end
T = all(t);
%@eof:3
T = all(t);
%@eof:3
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment