getting an error: this statement is incomplete
조회 수: 4 (최근 30일)
이전 댓글 표시
Ive been getting this error for the 'eval' line and i'm not sure in what way the statement is incomplete.
% Giving initial values vector (element for each layer) of each state variable.
for i=1:length(R(idR).St.StNames),
eval(strcat('R.St.', char(R(idR).St.StNames(i,:)),' = ', char(R(idR).StM(i,:))));
end
댓글 수: 0
채택된 답변
Guillaume
2017년 7월 22일
Important rule: Never use eval
And certainly not for generating dynamic fields when there is a more readable, lots easier to debug, alternative
Also a lot easier to debug: sprintf or, in recent versions, compose instead of concatenating bits of strings together.
Another issue is that it would appear that StNames is 2D (since you have written StNames(i, :)), yet you use length on it which will either return the number of rows or columns, whichever is greater. It's also not clear why it is converted to char.
One more potential problem, you're indexing R everywhere but in strcat('R.St.'. This is clearly not going to work. This is why we say not to use eval. You can't debug that line, and you get not help from the editor.
Not knowing what's in StNames, a guess of a fix:
for row = 1:size(R(idR).St.StNames, 1)
R(idR).St.(char(R(idR).St.StNames(row, :)) = char(R.(idR).StM(row, :);
end
Those char conversion are very suspicious though. Even if the above works, there's probably a much clearer way to write it.
I would also recommend that you use longer names for your fields, ones that have meaning. Finally, I would avoid mixing case as you have. It's a pain to write.
댓글 수: 1
Image Analyst
2017년 7월 22일
Also note Guillaume got rid of the comma at the end of the for line. In a test I did, that comma causes an error.
추가 답변 (1개)
John D'Errico
2017년 7월 22일
A textbook reason why it is a bad idea to create variable names on the fly. It creates code that is nasty to debug. The code will be slow, and it will be ugly unreadable stuff like this.
If you will write godawful stuff like this, then you need to learn to debug your code.
In the debugger, step in to see what those variables contain at that point. You will probably find that the line you are trying to execute (as it was created) is not correct MATLAB syntax.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!