Inconsisent(?) behaviour of str2num() with a particular usage

조회 수: 3 (최근 30일)
Dyuman Joshi
Dyuman Joshi 2023년 5월 21일
편집: Dyuman Joshi 2023년 10월 5일
The task in hand for me was to generate an empty array corresponding to the class/datatype of the input.
%Example 1
input = 'string';
output = char.empty
output = 0×0 empty char array
%Example 2
input = single(rand);
output = single.empty
output = 0×0 empty single matrix
My approach to this was to obtain the class of the input and use it with str2num -
input1 = "MATLAB";
str1 = sprintf('%s.empty', class(input1));
out1 = str2num(str1)
out1 = []
class(out1)
ans = 'double'
As you can see above, that the output is an empty array of double class and not of string class (as expected by me). This also occurs with char data-type
input2 = 'Batman'
input2 = 'Batman'
str2 = sprintf('%s.empty', class(input2));
out2 = str2num(str2)
out2 = []
class(out2)
ans = 'double'
I have checked for these datatypes - single, double, all integer classes, figure, datetime, duration, calendarDuration, timeseries, string and char. It works for all the datatyes except for the last two.
I expected the code to work for all classes.
My questions are - Did I expect wrongly?
If not, why does this give incorrect (edit - different) output for 2 data types?
  댓글 수: 4
Stephen23
Stephen23 2023년 5월 22일
"I should have done a better job at going through the documentation."
Both DGM and I were referring to the code itself, not the documentation. Try this:
type str2num
Dyuman Joshi
Dyuman Joshi 2023년 5월 22일
편집: Dyuman Joshi 2023년 5월 22일
Okay, type func returns the content of a function in text form. (I use "open func" for that, which directly opens the function file)
But yes, it can be seen from there, that for string/char/cell, it returns empty double.
Once again, thank you for your input.

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

채택된 답변

VBBV
VBBV 2023년 5월 21일
편집: VBBV 2023년 5월 21일
The function str2num can be used to convert the strings that inherently contain numbers to double class.
In your case there are no numbers in the outputs from sprintf function. Therefore, it results in empty [] which belongs to double class by default.
The reason why it worked for other data types is that they all belong to or contain numbers except for char & string
Hope this clarifies
  댓글 수: 6
VBBV
VBBV 2023년 5월 21일
@Dyuman Joshi, Figure and datetime inherits Number property and are considered or treated as numeric by Matlab, If you read the documentation of figure it is clearly stated.
figure(n) finds a figure in which the Number property is equal to n, and makes it the current figure. If no figure exists with that property value, MATLAB® creates a new figure and sets its Number property to n.
Hope this clarifies
Dyuman Joshi
Dyuman Joshi 2023년 5월 21일
편집: Dyuman Joshi 2023년 5월 21일
@Stephen23, thanks for your input. I guess there is no point of pondering over this.
@VBBV, thanks for the info. Yes, it does clarifies.
There seems to be an internal call/reference to Number property while calling datetime() as well (although it is not explicitly mentioned in the documentation of datetime() )
str2num('datetime')
ans = datetime
21-May-2023 14:56:42
Also, Thank you @DGM, for the explaination!

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

추가 답변 (1개)

Steven Lord
Steven Lord 2023년 5월 22일
If you want to evaluate the Static empty method of your class, don't use eval or str2num. Use feval or str2func.
classname = "string";
result = feval(classname + ".empty", 2, 0)
result = 2×0 empty string array
f = str2func(classname + ".empty");
result = f(0, 5)
result = 0×5 empty string array
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 5월 22일
편집: Dyuman Joshi 2023년 10월 5일
Unfortunately (or maybe fortunately), both the functions you have mentioned are not allowed on Cody.
Nonetheless, Thank you for the info @Steven Lord, always happy to learn something new.
I am inclined to believe feval() calls eval() internally. Would that be correct?

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

카테고리

Help CenterFile Exchange에서 Historical Contests에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by