How can I resolve the error 'Undefined variable "num2str" or class "num2str"' in a script using num2str function ?
Ältere Kommentare anzeigen
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 Kommentare
TastyPastry
am 8 Aug. 2017
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
am 8 Aug. 2017
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.
Antworten (1)
Jan
am 8 Aug. 2017
1 Stimme
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.
Kategorien
Mehr zu Variables finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!