Azzera filtri
Azzera filtri

str2num is returning and empty matrix: []

9 visualizzazioni (ultimi 30 giorni)
Matthew Noyes
Matthew Noyes il 15 Mag 2024
Spostato: Voss il 15 Mag 2024
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 Commenti
Stephen23
Stephen23 il 15 Mag 2024
Modificato: Stephen23 il 15 Mag 2024
"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
Voss il 15 Mag 2024
Spostato: Voss il 15 Mag 2024
You don't need to use cell2mat; just use curly braces to get the contents of a cell in a cell array.
For example:
datetempDat = {'5/8/2024 0:00,20.87';'5/8/2024 0:02,20.87'}
datetempDat = 2x1 cell array
{'5/8/2024 0:00,20.87'} {'5/8/2024 0:02,20.87'}
dat1 = datetempDat{1}
dat1 = '5/8/2024 0:00,20.87'

Accedi per commentare.

Risposta accettata

Voss
Voss il 15 Mag 2024
Modificato: Voss il 15 Mag 2024
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"
month = "5"
% but it's not the same as "5"
strcmp("5",month)
ans = logical
0
% and it returns [] when passed to str2num()
str2num(month)
ans = []
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),'')
month = "5"
% now it really is "5"
strcmp("5",month)
ans = logical
1
% and str2num() of it is numeric 5
str2num(month)
ans = 5
To be sure about the problem and an appropriate solution, upload your file using the paperclip button.
  2 Commenti
Matthew Noyes
Matthew Noyes il 15 Mag 2024
A hidden character I couldn't see was the issue! Thanks for pointing that out. Although, I had to take my string and use num2str("5") to find that there were two ASCII codes associated with it:
ans=
65279 53
53 is the ASCII decimal code for 5, while 65279 is the ASCII decimel code for a zero-width no-break space!! So I took this answer, removed the 65279 digit, and then converted it back to a double and that worked!
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. I stumbled upon that solution on accident.
Thanks again for the help!
Voss
Voss il 15 Mag 2024
Modificato: Voss il 15 Mag 2024
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"
m = "5"
num2str(m)
ans = '5'
char(m)
ans = '5'
m{1}
ans = '5'
isequal(num2str(m),char(m),m{1})
ans = logical
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))
ans = 1x2
65279 53
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
char(m)+0
ans = 1x2
65279 53
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
+char(m)
ans = 1x2
65279 53
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
double(m{1})
ans = 1x2
65279 53
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
m{1}+0
ans = 1x2
65279 53
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
+m{1}
ans = 1x2
65279 53
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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.

Accedi per commentare.

Più risposte (1)

Steven Lord
Steven Lord il 15 Mag 2024
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"
month = "5"
d = double(month)
d = 5
This can even handle some extra spaces
month2 = " 5 "
month2 = " 5 "
double(month2)
ans = 5

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by