确定输入“conscalc: calc”的类型时出错。无法将 coder.StructTypes 与不同的字段集联合
Posted
技术标签:
【中文标题】确定输入“conscalc: calc”的类型时出错。无法将 coder.StructTypes 与不同的字段集联合【英文标题】:Error determining type for input 'conscalc: calc '. Cannot union coder.StructTypes with different sets of fields 【发布时间】:2017-07-09 07:48:18 【问题描述】:当我尝试通过 Matlab 编码器应用程序将包含多个函数的 Matlab 程序作为一个组转换为 C++ 程序时,我收到一条错误消息: enter image description here.而变量calc是Matlab中的一个结构体。但是,如果我尝试使用 concalc 函数本身,则完全没有问题。 有什么问题?
这里是conscalc函数的代码:
function [calc] = conscalc(rho, gv, calc)
tp = 1 + rho;
sqrt2 = sqrt(2);
consadj = 2 ^ (0.5 * (1 - calc.alpha));
% initialization;
cv = zeros(gv.nacatlim, 1); % consumption equivalents of future expected marginal utility;
yac = zeros(gv.nacatlim, 1); % income (y) - end of period asset (a) - optimal consumption given end of period asse (c);
margu = zeros(gv.nvcatlim, gv.nacatlim); % marginal utility next period by next period survival status (1 - 3) and initial asset (1 - gv.nacatlim);
cons = zeros(gv.T - gv.beginage + 1, gv.nvcatlim, gv.nacatlim); % optimal consumption at the current period by age (25 - 99), survival status (1 - 3) and initial asset (1 - gv.nacatlim);
% simplified backward induction;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% calculate optimal consumption and marginal utility for the terminal age %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the optimal decision is to consume everything at the terminal age;
for vcat = 1: 3; % survival status;
for acat = 1 : gv.nacatlim; % asset;
y = calc.acats(acat, 1) + calc.income(vcat, gv.T - gv.beginage + 1); % total resources in the last period given initial asset and income;
mu = y ^ (calc.alpha - 1); % marginal utility given next period survival status and initial asset;
if vcat == 1; % married couple adjustment;
mu = consadj * mu;
end;
% save to marginal utility next period (when calculating backward to age - 1) for later calculations;
margu(vcat, acat) = mu;
end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% calculate optimal consumption and marginal utility for ages gv.t to gv.T - 1 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for age = gv.T - 1 : -1 : gv.beginage; % age;
for vcat = 1 : 3; % survival status;
y = calc.income(vcat, age - gv.beginage + 1); % income given survival status and age;
% calculate expected marginal utility next period given current period end of period asset;
for acat = 1 : gv.nacatlim; % asset;
mu = 0; % expected marginal utility next period given current period survival status and end of period asset;
for rcat = 1 : gv.nrcatlim; % asset return shock;
mur = 0; % marginal utility next period given current period survival status, end of period asset and asset return shock;
% (end of period asset + saving) * asset return shock is asset next period;
% interpolation;
% find corresponding asset grid point of the next period initial asset given current period end of period asset and asset return shock;
acatf = floor(calc.rtransa(acat, rcat));
if acatf >= gv.nacatlim;
acatf = gv.nacatlim - 1;
end;
fa = calc.rtransa(acat, rcat) - acatf;
for vcatt = 1 : 3; % survival status next period;
if vcatt == 1 || (vcat == 1 && age >= gv.surviveage); % the codes are not right. if vcat == 2/3, the program uses margu(1, acatf); should use margu(2/3, acatf); ???
mu0 = margu(vcatt, acatf);
mu1 = margu(vcatt, acatf + 1);
if mu0 <= 0 || mu1 <= 0;
fprintf('Interpolaton Error: Bad mu in rho section: %2d %2d %14.6e %14.6e %2d %2d %2d', ...
calc.obs, age, mu0, mu1, vcat, acat, rcat);
return;
end;
if fa <= 1;
murv = (1 - fa) * mu0 + fa * mu1;
else
if mu0 > mu1;
dmu = mu0 - mu1;
mufact = dmu / mu0;
murv = mu1 / (1 + (fa - 1) * mufact);
else
murv = mu1;
end;
end;
if vcat == 1 && age >= gv.surviveage; % both spouses alive;
mur = mur + calc.transrate(vcatt, age - gv.beginage + 1) * murv;
else
mur = mur + murv;
end;
end;
end;
mu = mu + calc.rprob(rcat, 1) * mur;
end;
% marginal utility this period should equal to the discounted expected marginal utility next period;
% convert optimal discounted expected marginal utility back to consumption level;
if vcat == 1; % both spouses alive;
cv(acat, 1) = sqrt2 * (mu / tp) ^ (1 / (calc.alpha - 1));
elseif vcat == 2 || vcat == 3; % only one spouse alive;
cv(acat, 1) = (mu * calc.srate(vcat - 1, age - gv.beginage + 1) / tp) ^ (1 / (calc.alpha - 1));
end;
yac(acat, 1) = y - calc.acats(acat, 1) - cv(acat, 1); % income - end of period asset - consumption;
end;
% find optimal consumption at the current period given initial asset;
k = 1; % initialize asset grid point;
for acat = 1 : gv.nacatlim; % asset;
nassets = - calc.acats(acat, 1); % - initial asset level at the current period;
% find how much asset left after consumption;
% - asset(t) = income - end of period asset(t) - optimal consumption(t) given end of period asset(t);
% interpolation;
if yac(k, 1) < nassets;
k = k - 1;
while k >= 1 && yac(k, 1) < nassets;
k = k - 1;
end;
if k < 1; % optimal to leave no assets to next period;
f = 0;
k = 1;
elseif k >= 1;
f = (yac(k, 1) - nassets) / (yac(k, 1) - yac(k + 1, 1));
end;
elseif yac(k, 1) >= nassets;
while k < gv.nacatlim && yac(k + 1, 1) >= nassets;
k = k + 1;
end;
if k > gv.nacatlim - 1; % requires extrapolation;
k = gv.nacatlim - 1;
if cv(k + 1, 1) > cv(k, 1);
f = (yac(k, 1) - nassets) / (yac(k, 1) - yac(k + 1, 1));
else
f = 1 + (yac(k + 1, 1) - nassets) / (calc.acats(k + 1, 1) - calc.acats(k, 1));
end;
elseif k <= gv.nacatlim - 1;
f = (yac(k, 1) - nassets) / (yac(k, 1) - yac(k + 1, 1));
end;
end;
c = y + calc.acats(acat, 1) - ((1 - f) * calc.acats(k, 1) + f * calc.acats(k + 1, 1)); % optimal consumption at the current period;
% calculate marginal utility at the current period given optimal consumption;
if vcat == 1; % married couple adjustment;
mu = consadj * c ^ (calc.alpha - 1);
elseif vcat == 2 || vcat == 3;
mu = c ^ (calc.alpha - 1);
end;
% save optimal consumption to corresponding optimal consumption matrix for later calculations;
cons(age - gv.beginage + 1, vcat, acat) = c; % optimal consumption at the current period;
margu(vcat, acat) = mu; % marginal utility next period (when calculating backward at age - 1), given survival status and initial asset;
end;
end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% assign the values to structure variable calc for future calculations %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
calc.cons = cons;
end
【问题讨论】:
使用脚本自动定义输入类型时出现错误。 【参考方案1】:该错误表明在自动输入类型定义期间,您的函数是使用具有不同字段集的结构调用的。
要进行调试,请将 breakpoint 放入 MATLAB 的入口点函数中,然后运行输入定义测试脚本。记下传入的结构以查看不匹配的根源。
【讨论】:
以上是关于确定输入“conscalc: calc”的类型时出错。无法将 coder.StructTypes 与不同的字段集联合的主要内容,如果未能解决你的问题,请参考以下文章
通过选定的星图对视觉和后端进行评级时出现问题 选择输入类型收音机的隐藏值
将输入字段聚焦在React组件中 - 尝试创建ref时出现类型错误