str2num is returning and empty matrix: []
6 次查看(过去 30 天)
显示 更早的评论
Problem:
I'm trying to convert a string containing a number into a double, but the output of str2num keeps giving me an empty matrix [].
Context:
I've imported data from an excel sheet with importdata(). That data comes in as a cell array. To extract data from a specific cell, I use cell2mat(). This yeilds numbers in the class: Char. (Example: '5'). I use convertCharsToString('5') to convert that character into a string. What I'm left with is ans = "5".
Now, I want to convert this "5" into a double. So, I used str2num(ans), and I get [] as the output (instead of 5 of class double).
I even tried this debugg:
a = "5";
strcmp(a,month)
and got a logical = 0...
Any ideas why this isn't woking?
2 个评论
Stephen23
2024-5-15
编辑:Stephen23
2024-5-15
"So, I used str2num(ans)"
Ugh, so you are relying on unreliable ANS ...
Take a look at your workspace, and see what ANS really is:
What makes you think that the text 'string' should be able to be converted into a number?
Solution: forget about ANS. Use a proper variable.
"I've imported data from an excel sheet with importdata()"
Forget about unreliable IMPORTDATA. Import the data properly using e.g. READTABLE... which also lets you import those timestamps as DATETIME objects and then access their YEAR, MONTH, etc properties. Much better than all of that fiddly buggy messing around with text that you are doing.
Please upload your data file by clicking the paperclip button.
采纳的回答
Voss
2024-5-15
编辑:Voss
2024-5-15
There may be a character you don't see, at the beginning of the file, which is included by importdata at the beginning of the first date and therefore is included at the beginning of the month string, e.g.:
% this looks the same as "5" when you see it in the command window
month = char(65279)+"5"
% but it's not the same as "5"
strcmp("5",month)
% and it returns [] when passed to str2num()
str2num(month)
If that's the case, you can remove it from month (or, better, remove it earlier in the process), e.g.:
% remove character 65279 from month
month = strrep(month,char(65279),'')
% now it really is "5"
strcmp("5",month)
% and str2num() of it is numeric 5
str2num(month)
To be sure about the problem and an appropriate solution, upload your file using the paperclip button.
2 个评论
Voss
2024-5-15
编辑:Voss
2024-5-15
You're welcome!
"I found it odd that I needed to take my string, and input it into num2str() in order to get out the associated ASCII codes though."
num2str() applied to a scalar string appears to convert it to a character vector, the same as char() or brace indexing would do, e.g.:
m = char(65279)+"5"
num2str(m)
char(m)
m{1}
isequal(num2str(m),char(m),m{1})
To get the character codes from a scalar string, here are a few equivalent ways, all of which involve operating on a character vector:
double(char(m))
char(m)+0
+char(m)
double(m{1})
m{1}+0
+m{1}
So num2str is not really necessary for getting the character codes from a scalar string, but getting the corresponding character vector and converting it to a numeric class (either explicitly or implicitly) is necessary.
更多回答(1 个)
Steven Lord
2024-5-15
Since month is a string containing the text representation of a number, you don't need to use str2num. Just call double on it.
month = "5"
d = double(month)
This can even handle some extra spaces
month2 = " 5 "
double(month2)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!