How can I resolve the error 'Undefined variable "num2str" or class "num2str"' in a script using num2str function ?
显示 更早的评论
While running the following script to return area of a circle, I am getting an error Undefined variable "num2str" or class "num2str"
radius=2.5;
Area= pi*2.5^2;
string=['The area of circle is ' num2str(Area)];
disp(string);
However, the script runs fine when I make the following changes
radius=2.5;
Area= pi*2.5^2;
s=num2str(Area);
string=['The area of circle is ' s];
disp(string);
Can anybody explain to me why is it showing the error in former and how is it resolved in the latter? I am using 2013a version.
2 个评论
TastyPastry
2017-8-8
I wouldn't expect either of those to error. Just to make sure, I ran it on 2013b and 2007b, both of those worked. Did you check your workspace to see if there were any variables named num2str or possibly you named one of your variables num2str before?
the cyclist
2017-8-8
Both worked for me. The num2str function is ancient, so I would not expect your issue to be related to version.
As an aside, I assume you actually want
Area= pi*radius^2;
since you went to the trouble of parameterizing it.
回答(1 个)
Jan
2017-8-8
1 个投票
I dare to answer the magic question: No, I'm convinced that your observation is incomplete. Copy&paste both codes from the forums display and run it in Matlab. They will work.
The problem you observed is not caused by a redefinition of num2str as a variable. I even do not believe, that 'Undefined variable "num2str" or class "num2str"' is the complete error message. Does the rest of the message reveal any details? Perhaps a "for class sym"?
The problem is hidden in some other code, which you did not show.
类别
在 帮助中心 和 File Exchange 中查找有关 Variables 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!