필터 지우기
필터 지우기

Why do I get an invalid text character error?

조회 수: 15 (최근 30일)
Eric
Eric 2023년 5월 5일
댓글: Walter Roberson 2023년 5월 6일
Hello! Here is the code:
V1 = [-3 20 3];
V2 = [3 10 3.5];
V3 = [-123 32 34];
syms vx vy vz real
vslk = [vx ; vy ; vz];
vfl = simplify(norm(vskl));
alfa = acos(vx/vfl);
beta = acos(vy/vfl);
gama = acos(vz/vfl);
for i=1:3
disp (['vectorul', num2str(i), ':']);
VX = eval(['V',i]);
vxn = VX(1);
vyn = VX(2);
vzn = VX(3);
vn=eval(subs(vfl, [vx vy vz], [vxn vyn vzn]));
disp(['modulul vectorului ', num2str(i), ': ', num2str(vn)]);
alfan = rad2deg(eval(subs(alfa, [vx vy vz], [vxn vyn vzn])));
betan = rad2deg(eval(subs(beta, [vx vy vz], [vxn vyn vzn])));
gaman = rad2deg(eval(subs(gama, [vx vy vz], [vxn vyn vzn])));
disp(['unghiul alfa al vectorului ', num2str(i), ': ', num2str(alfan), ' grade']);
disp(['unghiul beta al vectorului ', num2str(i), ': ', num2str(betan), ' grade']);
disp(['unghiul gama al vectorului ', num2str(i), ': ', num2str(gaman), ' grade']);
end
It gives me the following error:
Error using untitled4
Error: Invalid text character. Check for unsupported symbol, invisible
character, or pasting of non-ASCII characters.
How can I see which line has a problem?

답변 (2개)

Walter Roberson
Walter Roberson 2023년 5월 5일
for i=1:3
i will contain numeric values.
VX = eval(['V',i]);
When you use the [] operator between a character vector and a number, then the result is the same as if you had done
VX = eval(['V',char(i)]);
char(1) does not give you '1' (the character for the digit 1) as a result. char(1) gives you the character whose Unicode Code Point is 1 -- the second Unicode character (first is code point 0). That character is traditionally referred to as SOH, Start of Heading; https://www.compart.com/en/unicode/U+0001
How do you fix this problem? Easy: Don't Use eval.
V1 = [-3 20 3];
V2 = [3 10 3.5];
V3 = [-123 32 34];
Vs = {V1, V2, V3};
for i=1:3
disp (['vectorul', num2str(i), ':']);
VX = Vs{i};
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 5월 5일
There is no documented eval() function for symbolic expressions.
vn=eval(subs(vfl, [vx vy vz], [vxn vyn vzn]));
could do pretty much anything and it would not be a "bug" because the behaviour is not defined by Mathworks.
In practice, eval() of a symbolic expression is treated the same as eval() of char() of the symbolic expression, as if you had written
vn=eval(char(subs(vfl, [vx vy vz], [vxn vyn vzn])));
That might sound reasonable at first, but the problem is that the char() of a symbolic expression is mostly for display purposes, and is not certain to happen to be valid syntax for internal symbolic expressions and is not certain to happen to be valid syntax for MATLAB. There are also some cases where the parameters for symbolic functions are in a different order than the parameters for the similarly-named numeric function; for example historically the parameters for the symbolic function beta were different than the order for the numeric function beta. Using char() produces a human-readable representation, and is not designed to produce something that can be executed numerically.
So, do not use eval() on symbolic expressions. It is undefined, and will give you error messages in some cases, and will silently succeed but give you undesired results in other cases.
Use subs() instead to get down to symbolic numbers, and then double() the symbolic number to get a double precision number.

댓글을 달려면 로그인하십시오.


Atsushi Ueno
Atsushi Ueno 2023년 5월 5일
You should change the value i to string data.
VX = eval(['V',num2str(i)])
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 5월 6일
To be fair, for this particular operation, selecting one of the V1, V2, V3 variables, that part does not involve any operations on symbolic expressions. The
vn=eval(subs(vfl, [vx vy vz], [vxn vyn vzn]));
is a different part of the code. eval() is being abused in two different ways in the original code.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by