我想从数组中提取值并在字符串表达式中使用它们

时间:2014-01-21 22:10:11

标签: arrays matlab for-loop

我正在尝试进行非线性拟合的for循环,其中一个系数随每次迭代而改变值。这些值位于名为YoungMod的数组中。我有:

for k=1:size(DataAll,3)
    ft = fittype('E1*(YoungMod(k))-b*t','coefficients',{'E1','b'},'independent', 't','dependent','depth','options',fo);
    [FittedData{k},gof{k}] = fit(DataAll(:,3,k),DataAll(:,1,k),ft1); %{k});
    coeffs{k}=coeffvalues(FittedData{k});
end

我收到的错误消息是:

    Error using fittype>iTestCustomModelEvaluation (line 726)
    Expression
    E1*(YoungMod(k))-b*t
    is not a valid MATLAB expression, has non-scalar coefficients, or cannot be evaluated:
    Error in fittype expression ==>
   (E1*YoungMod(k))-b*t
    ??? Undefined function 'YoungMod' for input arguments of type 'double'.

对我来说,看起来问题是字符串正在读取YoungMod *(k)而不是取YoungMod数组的k值。非常感谢帮助!

2 个答案:

答案 0 :(得分:0)

尝试这样做以确保k被评估为变量而不是字符串:

ft = fittype(['E1*(YoungMod(' num2str(k) '))-b*t'],'coefficients',{'E1','b'},'independent', 't','dependent','depth','options',fo);

答案 1 :(得分:0)

我设法使用虚拟变量和“问题”解决了这个问题。 fittype选项:

for k=1:size(DataAll,3)
    ym = YoungMod(k)
    ft = fittype('E1*(ym)-b*t','coefficients',{'E1','b'},'independent', 't','dependent','depth','options',fo,'problem','ym');
    [FittedData{k},gof{k}] = fit(DataAll(:,3,k),DataAll(:,1,k),ft,'problem',ym);
    coeffs{k}=coeffvalues(FittedData{k});
end