MATLAB Answers

eval([ans '=' num2str(x0(i)) ';']); gives an error "The expression to the left of the equals sign is not a valid target for an assignment". what could be the problem?

20 ビュー (過去 30 日間)
Balaji Jayaraman
Balaji Jayaraman 2017 年 11 月 8 日
Commented: Adam 2017 年 11 月 8 日
eval([ans '=' num2str(x0(i)) ';']); gives an error "The expression to the left of the equals sign is not a valid target for an assignment". what could be the problem?

件の回答 (2)

Guillaume
Guillaume 2017 年 11 月 8 日
編集済み: Guillaume 2017 年 11 月 8 日
There are many thing wrong with
eval([ans '=' num2str(x0(i)) ';'])
  • eval expect a string or char array. As per Mischa's answer the whole expression probably needs to be enclosed in quote.
  • do not use ans as a variable name. It's a special variable used by matlab.
  • and worst of all, is the use of eval. There's normally very little reason to use eval, in the above case, there's absolutely no reason, other than making the code harder to debug, more difficult to read and obviously harder to use since you can't get the syntax right
SomeVariableNameOtherThanANS = num2str(xi(0));
would do exactly the same.

  0 件のコメント

サインイン to comment.


Mischa Kim
Mischa Kim 2017 年 11 月 8 日
Just wondering if the following would do the trick...
eval('ans = num2str(x0(i));')

  4 件のコメント

表示 1 件の古いコメント
Mischa Kim
Mischa Kim 2017 年 11 月 8 日
I do not have enough information here to judge what is required and what is not. The question posed contains one single command.
Balaji Jayaraman
Balaji Jayaraman 2017 年 11 月 8 日
it is a code written for gradient descent function.
function [xsol, fsol] = GradientDescent(f,varx,x0,epis,gamma)
for i=1:length(varx)
nabf(i) = diff(f,varx(i));
end
nabf = vpa(nabf,5);
for i=1:length(varx)
char(varx(i));
eval([ans '=' num2str(x0(i)) ';']);
end
nabfloop = subs(nabf)';
while(double(norm(nabfloop))>=epis)
x0 = x0 - gamma*double(nabfloop);
x0
for i=1:length(varx)
char(varx(i));
eval([ans '=' num2str(x0(i)) ';']);
end
nabfloop = subs(nabf)';
end
xsol = x0;
for i=1:length(varx)
char(varx(i));
eval([ans '=' num2str(x0(i)) ';']);
end
fsol = double(subs(f));
end
Adam
Adam 2017 年 11 月 8 日
Most of that code appears to do nothing or create variables that nothing is done with!

サインイン to comment.

サインイン してこの質問に回答します。

タグ


Translated by