Commit bc088607 authored by Houtan Bastani's avatar Houtan Bastani

copy over OLS infrastructure

parent edba57bb
function ds = dyn_ols(ds, fitted_names_dict, eqtags)
% function ds = dyn_ols(ds, fitted_names_dict, eqtags)
% Run OLS on chosen model equations; unlike olseqs, allow for time t
% endogenous variables on LHS
%
% INPUTS
% ds [dseries] data
% fitted_names_dict [cell] Nx2 or Nx3 cell array to be used in naming fitted
% values; first column is the equation tag,
% second column is the name of the
% associated fitted value, third column
% (if it exists) is the function name of
% the transformation to perform on the
% fitted value.
% eqtags [cellstr] names of equation tags to estimate. If empty,
% estimate all equations
%
% OUTPUTS
% ds [dseries] data updated with fitted values
%
% SPECIAL REQUIREMENTS
% none
% Copyright (C) 2017-2018 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
global M_ oo_ options_
assert(nargin >= 1 && nargin <= 3, 'dyn_ols: takes between 1 and 3 arguments');
assert(isdseries(ds), 'dyn_ols: the first argument must be a dseries');
jsonfile = [M_.fname '_original.json'];
if exist(jsonfile, 'file') ~= 2
error('Could not find %s! Please use the json=compute option (See the Dynare invocation section in the reference manual).', jsonfile);
end
%% Get Equation(s)
jsonmodel = loadjson(jsonfile);
jsonmodel = jsonmodel.model;
if nargin == 1
[lhs, rhs, lineno, sample, tags] = getEquationsByTags(jsonmodel);
fitted_names_dict = {};
else
assert(isempty(fitted_names_dict) || ...
(iscell(fitted_names_dict) && ...
(size(fitted_names_dict, 2) == 2 || size(fitted_names_dict, 2) == 3)), ...
'dyn_ols: the second argument must be an Nx2 or Nx3 cell array');
if nargin == 2
[lhs, rhs, lineno, sample, tags] = getEquationsByTags(jsonmodel);
else
[lhs, rhs, lineno, sample, tags] = getEquationsByTags(jsonmodel, 'name', eqtags);
end
if isempty(lhs)
disp('dyn_ols: Nothing to estimate')
return
end
end
%% Estimation
M_endo_exo_names_trim = [M_.endo_names; M_.exo_names];
regex = strjoin(M_endo_exo_names_trim(:,1), '|');
mathops = '[\+\*\^\-\/\(\)]';
M_param_names_trim = cellfun(@strtrim, num2cell(M_.param_names, 2), 'UniformOutput', false);
for i = 1:length(lhs)
%% Construct regression matrices
rhs_ = strsplit(rhs{i}, {'+','-','*','/','^','log(','exp(','(',')'});
rhs_(cellfun(@(x) all(isstrprop(x, 'digit')), rhs_)) = [];
vnames = setdiff(rhs_, M_.param_names);
if ~isempty(regexp(rhs{i}, ...
['(' strjoin(vnames, '\\(\\d+\\)|') '\\(\\d+\\))'], ...
'once'))
error(['dyn_ols: you cannot have leads in equation on line ' ...
lineno{i} ': ' lhs{i} ' = ' rhs{i}]);
end
pnames = intersect(rhs_, M_.param_names);
vnames = cell(1, length(pnames));
splitstrings = cell(length(pnames), 1);
X = dseries();
for j = 1:length(pnames)
createdvar = false;
pregex = [...
mathops pnames{j} mathops ...
'|^' pnames{j} mathops ...
'|' mathops pnames{j} '$' ...
];
[startidx, endidx] = regexp(rhs{i}, pregex, 'start', 'end');
assert(length(startidx) == 1);
if rhs{i}(startidx) == '*' && rhs{i}(endidx) == '*'
vnamesl = getStrMoveLeft(rhs{i}(1:startidx-1));
vnamesr = getStrMoveRight(rhs{i}(endidx+1:end));
vnames{j} = [vnamesl '*' vnamesr];
splitstrings{j} = [vnamesl '*' pnames{j} '*' vnamesr];
elseif rhs{i}(startidx) == '*'
vnames{j} = getStrMoveLeft(rhs{i}(1:startidx-1));
splitstrings{j} = [vnames{j} '*' pnames{j}];
elseif rhs{i}(endidx) == '*'
vnames{j} = getStrMoveRight(rhs{i}(endidx+1:end));
splitstrings{j} = [pnames{j} '*' vnames{j}];
if rhs{i}(startidx) == '-'
vnames{j} = ['-' vnames{j}];
splitstrings{j} = ['-' splitstrings{j}];
end
elseif rhs{i}(startidx) == '+' ...
|| rhs{i}(startidx) == '-' ...
|| rhs{i}(endidx) == '+' ...
|| rhs{i}(endidx) == '-'
% intercept
createdvar = true;
if any(strcmp(M_endo_exo_names_trim, 'intercept'))
[~, vnames{j}] = fileparts(tempname);
vnames{j} = ['intercept_' vnames{j}];
assert(~any(strcmp(M_endo_exo_names_trim, vnames{j})));
else
vnames{j} = 'intercept';
end
splitstrings{j} = vnames{j};
else
error('dyn_ols: Shouldn''t arrive here');
end
if createdvar
if rhs{i}(startidx) == '-'
Xtmp = dseries(-ones(ds.nobs, 1), ds.firstdate, vnames{j});
else
Xtmp = dseries(ones(ds.nobs, 1), ds.firstdate, vnames{j});
end
else
Xtmp = eval(regexprep(vnames{j}, regex, 'ds.$&'));
Xtmp.rename_(vnames{j});
end
X = [X Xtmp];
end
lhssub = getRhsToSubFromLhs(ds, rhs{i}, regex, [splitstrings; pnames]);
residuals = setdiff(intersect(rhs_, M_.exo_names), ds.name);
assert(~isempty(residuals), ['No residuals in equation ' num2str(i)]);
assert(length(residuals) == 1, ['More than one residual in equation ' num2str(i)]);
Y = eval(regexprep(lhs{i}, regex, 'ds.$&'));
for j = 1:lhssub.vobs
Y = Y - lhssub{j};
end
fp = max(Y.firstobservedperiod, X.firstobservedperiod);
lp = min(Y.lastobservedperiod, X.lastobservedperiod);
if ~isempty(sample{i})
if fp > sample{i}(1) || lp < sample{i}(end)
warning(['The sample over which you want to estimate contains NaNs. '...
'Adjusting estimation range to be: ' fp.char ' to ' lp.char])
else
fp = sample{i}(1);
lp = sample{i}(end);
end
end
Y = Y(fp:lp);
X = X(fp:lp).data;
%% Estimation
% From LeSage, James P. "Applied Econometrics using MATLAB"
[nobs, nvars] = size(X);
oo_.ols.(tags{i}).dof = nobs - nvars;
% Estimated Parameters
[q, r] = qr(X, 0);
xpxi = (r'*r)\eye(nvars);
oo_.ols.(tags{i}).beta = r\(q'*Y.data);
for j = 1:length(pnames)
keyboard
M_.params(strcmp(M_param_names_trim, pnames{j})) = oo_.ols.(tags{i}).beta(j);
end
% Yhat
idx = 0;
yhatname = [tags{i} '_FIT'];
if ~isempty(fitted_names_dict)
idx = strcmp(fitted_names_dict(:,1), tags{i});
if any(idx)
yhatname = fitted_names_dict{idx, 2};
end
end
oo_.ols.(tags{i}).Yhat = dseries(X*oo_.ols.(tags{i}).beta, fp, yhatname);
if any(idx) ...
&& length(fitted_names_dict(idx, :)) == 3 ...
&& ~isempty(fitted_names_dict{idx, 3})
oo_.ols.(tags{i}).Yhat = ...
eval([fitted_names_dict{idx, 3} '(oo_.ols.(tags{' num2str(i) '}).Yhat)']);
end
% Residuals
oo_.ols.(tags{i}).resid = Y - oo_.ols.(tags{i}).Yhat;
% Correct Yhat reported back to user for given
for j = 1:lhssub.vobs
oo_.ols.(tags{i}).Yhat = oo_.ols.(tags{i}).Yhat + lhssub{j}(fp:lp);
end
ds = [ds oo_.ols.(tags{i}).Yhat];
%% Calculate statistics
% Estimate for sigma^2
SS_res = oo_.ols.(tags{i}).resid.data'*oo_.ols.(tags{i}).resid.data;
oo_.ols.(tags{i}).s2 = SS_res/oo_.ols.(tags{i}).dof;
% R^2
ym = Y.data - mean(Y);
SS_tot = ym'*ym;
oo_.ols.(tags{i}).R2 = 1 - SS_res/SS_tot;
% Adjusted R^2
oo_.ols.(tags{i}).adjR2 = oo_.ols.(tags{i}).R2 - (1 - oo_.ols.(tags{i}).R2)*nvars/(oo_.ols.(tags{i}).dof-1);
% Durbin-Watson
ediff = oo_.ols.(tags{i}).resid.data(2:nobs) - oo_.ols.(tags{i}).resid.data(1:nobs-1);
oo_.ols.(tags{i}).dw = (ediff'*ediff)/SS_res;
% Standard Error
oo_.ols.(tags{i}).stderr = sqrt(oo_.ols.(tags{i}).s2*diag(xpxi));
% T-Stat
oo_.ols.(tags{i}).tstat = oo_.ols.(tags{i}).beta./oo_.ols.(tags{i}).stderr;
%% Print Output
if ~options_.noprint
if nargin == 3
title = ['OLS Estimation of equation ''' tags{i} ''' [name = ''' tags{i} ''']'];
else
title = ['OLS Estimation of equation ''' tags{i} ''''];
end
preamble = {sprintf('Dependent Variable: %s', lhs{i}), ...
sprintf('No. Independent Variables: %d', nvars), ...
sprintf('Observations: %d from %s to %s\n', nobs, fp.char, lp.char)};
afterward = {sprintf('R^2: %f', oo_.ols.(tags{i}).R2), ...
sprintf('R^2 Adjusted: %f', oo_.ols.(tags{i}).adjR2), ...
sprintf('s^2: %f', oo_.ols.(tags{i}).s2), ...
sprintf('Durbin-Watson: %f', oo_.ols.(tags{i}).dw)};
dyn_table(title, preamble, afterward, vnames, ...
{'Coefficients','t-statistic','Std. Error'}, 4, ...
[oo_.ols.(tags{i}).beta oo_.ols.(tags{i}).tstat oo_.ols.(tags{i}).stderr]);
end
end
end
function dyn_table(title, preamble, afterward, rows, cols, indent, data)
%function dyn_table(title, rows, cols, indent, data)
% Print Table
%
% INPUTS
% title [char]
% preamble [cell string]
% afterward [cell string]
% rows [cell string]
% cols [cell string]
% indent [integer]
% data [matrix]
%
% OUTPUTS
% None
%
% SPECIAL REQUIREMENTS
% none
% Copyright (C) 2017 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
assert(ischar(title), 'title must be a char')
assert(iscellstr(preamble) && iscellstr(afterward) && iscellstr(rows) && iscellstr(cols), ...
'preamble, afterward, rows, and cols must be cell arrays of strings')
assert(size(data, 1) == length(rows), 'must have the same number of rows')
assert(size(data, 2) == length(cols), 'must have the same number of columns')
assert(isint(indent), 'indent must be an integer')
skipline(3)
%% Print Output
rowstrlens = cellfun(@length, rows);
colstrlens = cellfun(@length, cols);
maxrowstrlen = max(rowstrlens);
colbegin = repmat(' ', 1, 2*indent + maxrowstrlen);
colrow = sprintf('%s', colbegin);
colrow2 = colrow;
format = [' %-' num2str(maxrowstrlen) 's'];
for i = 1:length(cols)
precision = 12;
if colstrlens(i) < precision
colrow = [colrow repmat(' ', 1, floor((precision-mod(colstrlens(i), precision))/2)) cols{i} repmat(' ', 1, ceil((precision-mod(colstrlens(i), precision))/2))];
colrow2 = [colrow2 repmat('_', 1, precision)];
else
colrow = [colrow cols{i}];
colrow2 = [colrow2 repmat('_', 1, colstrlens(i))];
precision = colstrlens(i);
end
if i ~= length(cols)
colrow = [colrow repmat(' ', 1, indent)];
colrow2 = [colrow2 repmat(' ', 1, indent)];
end
format = [format repmat(' ', 1, indent) '%' sprintf('%d.5f', precision)];
end
% Center title
if length(title) >= length(colrow)
fprintf('%s\n\n', title)
else
fprintf('%s%s\n\n', repmat(' ', 1, floor((length(colrow)+indent-length(title))/2)), title);
end
spaces = repmat(' ', 1, indent);
for i = 1:length(preamble)
fprintf('%s%s\n', spaces, preamble{i});
end
fprintf('%s\n', colrow);
fprintf('%s\n\n', colrow2);
format = [format '\n'];
for i = 1:length(rows)
fprintf(format, rows{i}, data(i, :));
end
fprintf('\n');
for i = 1:length(afterward)
fprintf('%s%s\n', spaces, afterward{i});
end
fprintf('%s\n\n', repmat('_', 1, length(colrow2)));
function [lhs, rhs, linenum, sample, tagvalue] = getEquationsByTags(jsonmodel, varargin)
%function [lhs, rhs, linenum, sample] = getEquationByTag(jsonmodel, varargin)
% Return the lhs, rhs of an equation and the line it was defined
% on given its tag
%
% INPUTS
% jsonmodel [string] JSON representation of model block
% varargin [string or cellstring arrays] tagname and tagvalue for
% eqs to get
%
% OUTPUTS
% lhs [cellstring array] left hand side of eq
% rhs [cellstring array] right hand side of eq
% linenum [cellstring array] eq line in .mod file
% sample [cell array of dates] sample range
% tagvalue [cellstring array] tags associated with equations
%
% SPECIAL REQUIREMENTS
% none
% Copyright (C) 2017 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
assert(nargin == 1 || nargin == 3, 'Incorrect number of arguments passed to getEquationsByTags');
if nargin == 1
lhs = cell(1, length(jsonmodel));
rhs = cell(1, length(jsonmodel));
linenum = cell(1, length(jsonmodel));
sample = cell(1, length(jsonmodel));
tagvalue = cell(1, length(jsonmodel));
for i=1:length(jsonmodel)
lhs{i} = jsonmodel{i}.lhs;
rhs{i} = jsonmodel{i}.rhs;
linenum{i} = jsonmodel{i}.line;
if isfield(jsonmodel{i}, 'tags') && ...
isfield(jsonmodel{i}.tags, 'name')
tagvalue{i} = jsonmodel{i}.tags.('name');
else
tagvalue{i} = ['eq_line_no_' num2str(linenum{i})];
end
if isfield(jsonmodel{i}, 'tags')
if isfield(jsonmodel{i}.tags, 'sample')
tmp = strsplit(jsonmodel{i}.tags.sample, ':');
sample{i} = dates(tmp{1}):dates(tmp{2});
end
else
tagvalue{i} = ['eq_line_no_' num2str(linenum{i})];
end
end
return
end
tagname = varargin{1};
tagvalue = varargin{2};
assert(ischar(tagname), 'Tag name must be a string');
assert(ischar(tagvalue) || iscell(tagvalue), 'Tag value must be a string or a cell string array');
if ischar(tagvalue)
tagvalue = {tagvalue};
end
lhs = cell(1, length(tagvalue));
rhs = cell(1, length(tagvalue));
linenum = cell(1, length(tagvalue));
sample = cell(1, length(tagvalue));
idx2rm = [];
for j = 1:length(tagvalue)
for i=1:length(jsonmodel)
if isfield(jsonmodel{i}, 'tags') && ...
isfield(jsonmodel{i}.tags, tagname) && ...
strcmp(jsonmodel{i}.tags.(tagname), tagvalue{j})
lhs{j} = jsonmodel{i}.lhs;
rhs{j} = jsonmodel{i}.rhs;
linenum{j} = jsonmodel{i}.line;
if isfield(jsonmodel{i}.tags, 'sample')
tmp = strsplit(jsonmodel{i}.tags.sample, ':');
sample{j} = dates(tmp{1}):dates(tmp{2});
end
if ~any(cellfun(@isempty, lhs))
return
end
break
end
end
if isempty(rhs{j})
warning(['getEquationsByTags: No equation tag found by the name of ''' tagvalue{j} ''''])
idx2rm = [idx2rm j];
end
end
if ~isempty(idx2rm)
lhs(:,idx2rm) = [];
rhs(:,idx2rm) = [];
linenum(:,idx2rm) = [];
sample(:,idx2rm) = [];
tagvalue(:,idx2rm) = [];
end
end
\ No newline at end of file
function lhssub = getRhsToSubFromLhs(ds, rhs, regex, splits)
%function lhssub = getRhsToSubFromLhs(ds, rhs, regex, splits)
% Helper function that identifies variables on RHS that need to be
% subtracted from LHS of OLS-style equation
%
% INPUTS
% ds [dseries] data
% rhs [string] RHS as a string
% regex [string] regex expressing valid list of variables
% splits [cell array] strings to split out of equation on RHS
%
% OUTPUTS
% lhssub [dseries] data to subtract from LHS
%
% SPECIAL REQUIREMENTS
% none
% Copyright (C) 2017-2018 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
global M_
assert(isdseries(ds), 'The first argument must be a dseries');
assert(ischar(rhs), 'The second argument must be a string');
assert(ischar(regex), 'The third argument must be a string');
assert(iscell(splits), 'The fourth argument must be a cell');
lhssub = dseries();
rhs_ = strsplit(rhs, splits);
for j = 1:length(rhs_)
rhsj = rhs_{j};
while ~isempty(rhsj)
minusstr = '';
if strcmp(rhsj(1), '-') || strcmp(rhsj(1), '+')
if length(rhsj) == 1
break
end
if strcmp(rhsj(1), '-')
minusstr = '-';
end
rhsj = rhsj(2:end);
end
str = getStrMoveRight(rhsj);
if ~isempty(str)
try
lhssub = [lhssub eval(regexprep([minusstr str], regex, 'ds.$&'))];
lhssub.rename_(lhssub{lhssub.vobs}.name{:}, [minusstr str]);
catch
if ~any(strcmp(M_.exo_names, str))
error(['getRhsToSubFromLhs: problem evaluating ' minusstr str]);
end
end
rhsj = rhsj(length(str)+1:end);
end
end
end
end
\ No newline at end of file
function retval = getStrMoveLeft(str)
%function retval = getStrMoveLeft(str)
% Helper function common to OLS routines. Given a string finds expr
% moving from right to left (potentially contained in parenthesis) until
% it gets to the + or -. e.g., given: str =
% 'res_9+DE_SIN*param+log(DE_SIN(-1))', returns 'log(DE_SIN)'
%
% INPUTS
% str [string] string
%
% OUTPUTS
% none
%
% SPECIAL REQUIREMENTS
% none
% Copyright (C) 2017 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
if isempty(str)
retval = '';
return
end
mathidxs = regexp(str, '[\+\-]');
closedidxs = strfind(str, ')');
if isempty(closedidxs) ...
|| (~isempty(mathidxs) ...
&& max(mathidxs) > max(closedidxs))
if isempty(mathidxs)
retval = str;
else
if str(max(mathidxs)) == '-'
retval = str(max(mathidxs):end);
else
retval = str(max(mathidxs)+1:end);
end
end
else
closedidxs = [(length(closedidxs):-1:1)' closedidxs'];
openidxs = strfind(str, '(');
openidxs = [(length(openidxs):-1:1)' openidxs'];
assert(rows(closedidxs) == rows(openidxs));
for i = rows(openidxs):-1:1
openparenidx = find(openidxs(i, 2) < closedidxs(:, 2), 1, 'first');
if openidxs(i, 1) == closedidxs(openparenidx, 1)
break
end
end
retval = str(openidxs(openparenidx, 2):end);
if openidxs(openparenidx, 2) ~= 1
if isempty(regexp(str(openidxs(openparenidx, 2) - 1), '[\+\-]', 'once'))
retval = [getStrMoveLeft(str(1:openidxs(openparenidx, 2) - 2)) ...
str(openidxs(openparenidx, 2) - 1) ...
retval];
end
end
if strfind(str, ['-' retval])
retval = ['-' retval];
end
end
function retval = getStrMoveRight(str)
%function retval = getStrMoveRight(str)
% Helper function common to OLS routines. Given a string finds expr
% moving from left to right (potentially contained in parenthesis) until
% it gets to the + or -. e.g., given: str =
% '(log(DE_SIN(-1))-log(DE_SIN))+anotherparam1*log(DE_SIN)', returns
% '(log(DE_SIN(-1))-log(DE_SIN))'
%
% INPUTS
% str [string] string
%
% OUTPUTS
% none
%
% SPECIAL REQUIREMENTS
% none
% Copyright (C) 2017 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
if isempty(str)
retval = '';
return
end
mathidxs = regexp(str, '[\+\-]');
openidxs = strfind(str, '(');
if isempty(openidxs) ...
|| (~isempty(mathidxs) ...
&& min(mathidxs) < min(openidxs))
if isempty(mathidxs)
retval = str;
else
retval = str(1:min(mathidxs)-1);
end
else
openidxs = [(1:length(openidxs))' openidxs'];
closedidxs = strfind(str, ')');
closedidxs = [(1:length(closedidxs))' closedidxs'];
assert(rows(openidxs) == rows(closedidxs));
for i = 1:rows(closedidxs)
closedparenidx = sum(openidxs(:, 2) < closedidxs(i, 2));
if openidxs(closedparenidx, 1) == closedidxs(i, 1)
break;
end
end
retval = str(1:closedidxs(closedparenidx, 2));
if length(str) > closedidxs(closedparenidx, 2) + 1
if any(strfind('*^/', str(closedidxs(closedparenidx, 2) + 1)))
retval = [retval ...
str(closedidxs(closedparenidx, 2) + 1) ...
getStrMoveRight(str(closedidxs(closedparenidx, 2) + 2:end))];
end
end
end
Markdown is supported
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