eval(sprintf('left = X+X%d+X%d+​X%d+X%d+X%​d+X%d', i==1:6)); throws error Undefined function or variable 'X0'. why

조회 수: 2 (최근 30일)
In my script I try to automate the additon of the variables but it is throwing error. please help
  댓글 수: 1
James Tursa
James Tursa 2019년 10월 29일
Use 1:6 instead of i==1:6
But, seriously, you should not be doing this at all. Heed all of the advice below.

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

답변 (3개)

Daniel M
Daniel M 2019년 10월 29일
  댓글 수: 2
ARAVINTH DESIKAN
ARAVINTH DESIKAN 2019년 10월 29일
what are the alternatives to eval function then can you tell me I wnat dynamic variables to be created from a cell
Daniel M
Daniel M 2019년 10월 29일
You should spend some time and read through that link a little deeper.

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


Walter Roberson
Walter Roberson 2019년 10월 29일
i==1:6
returns a vector of values that are all either 0 (not equal) or 1 (equal) . You slide those 0 and 1 into variable names. If i is a scalar, you can be sure that at most one of the locations is 1; if i is a row vector of length 6 then there might be more than one location with 1.
Therefore you are constructing an expression with variable X, and variable X0, and possibly also variable X1. You would get an error if variable X0 did not exist.
It is far from clear why you would want to use that code.
  댓글 수: 3
Walter Roberson
Walter Roberson 2019년 10월 29일
We do not know what you are trying to do. Are you trying to do the assignment
left = X + X1 + X2 + X3 + X4 + X5 + X6
if so then create a vector
Xvals = [X, X1, X2, X3, X4, X5, X6 ... up to maximum possible]
after which you can sum(Xvals(1:6)),
If you are not using scalars but you are using arrays that are the same size, then
nd = ndims(X);
Xvals = cat(nd+1, X, X1, X2, X3, X4, X5, X6 ... up to maximum possible);
after which you can sum(Xvals,nd+1) if you are doing all of them. If you are not doing all of them then it is possible to do, but it is easier if you know the maximum dimension ahead of time, such as sum(Xvals(:,:,1:6), 3) for the case the values are 2D.
ARAVINTH DESIKAN
ARAVINTH DESIKAN 2019년 10월 29일
left = X + X1 + X2 + X3 + X4 + X5 + X6
I want to automate this for n number of variables

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


Bhaskar R
Bhaskar R 2019년 10월 29일
I support Daniel M answer. But if you need answer anyway these are the resolutions
From your question it is clear that X0 is not initializied that means you need to initialize each and every variable initialized before the eval function.
Even if your all initializations are correct, the evaluation is giving error as non-ASCII characters contained string.
>> txt = sprintf('left = X+X%d+X%d+​X%d+X%d+X%​d+X%d', i==1:6) % took sprintf seperate as seperate string
>> txt =
'left = X+X0+X1+​X0+X0+X'
>> ASCII_values = double(txt); % ASCII values of the string
>> ASCII_values(16) % this is out of ASCII chacter
ans =
8203
>> txt(16) % this is empty string in the sprintf string
ans =
'​'
>> txt(16) = []; % you need to remove that non ASCII from the string so that you can apply eval cammand over there
For this you need to make additional programming
  댓글 수: 2
ARAVINTH DESIKAN
ARAVINTH DESIKAN 2019년 10월 29일
I have values coming from array ao.
the length of ao is 6
for i=1:length(ao)
eval(sprintf('X%d = [A%d(1)]', i,i));
eval(sprintf('Y%d = [A%d(2)]', i,i));
eval(sprintf('W%d = [A%d(3)]', i,i));
eval(sprintf('H%d = [A%d(4)]', i,i));
end
now I want to add x1 to length of ao automatically can you help
Walter Roberson
Walter Roberson 2019년 10월 29일
The second last % in 'left = X+X%d+X%d+​X%d+X%d+X%​d+X%d' is followed by char(8203) "zero width space" .
Sometimes the mechanism that creates titles for here introduces odd characters that are not in the original text.

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

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by