How to convert char data to double format?
19 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello!
I have data in the following char format.
The size of the data is [2x72], and I want to store each element, including the spaces,
in a matrix to create a double-format data with the same size.
val = ['11 444 214141111 11111111111114111111111 1111111113111111111 111'
'11 444 214141113 11111311111114111111121 1111111123111111111 111' ]; % char
When I use str2double, it returns NaN.
Could you please tell me how I can save this data?
1 commentaire
Stephen23
le 20 Juil 2023
Déplacé(e) : Stephen23
le 21 Juil 2023
"The size of the data is [2x72],..."
The data you provided is actually a 2x64 char array.
"I want to store each element, including the spaces, in a matrix to create a double-format data with the same size."
Here it is, guaranteed to be exactly the same size as your character array. But I doubt you will be very happy
val = ['11 444 214141111 11111111111114111111111 1111111113111111111 111';'11 444 214141113 11111311111114111111121 1111111123111111111 111']
num = double(val)
Réponses (1)
Walter Roberson
le 20 Juil 2023
format long g
val = ['11 444 214141111 11111111111114111111111 1111111113111111111 111'
'11 444 214141113 11111311111114111111121 1111111123111111111 111' ]; % char
result_double = cell2mat(cellfun(@str2double,regexp(cellstr(val), '\s+', 'split'),'UniformOutput',false))
%or
result_u64 = cell2mat(cellfun(@(S) sscanf(S, '%ld').', cellstr(val), 'uniform', 0))
However:
- you cannot include spaces in the numeric array. In order to include spaces, you would need to convert back to character or string() unless what you wanted was a cell array that alternates double and char
- your values such as 11111311111114111111121 exceed the largest possible integer exactly representable in double precision, and also exceed the largest possible 64 bit integer, so you will not be able to represent the values exactly -- not unless you use symbolic numbers
result_sym = str2sym("[" + cellstr(val) + "]")
0 commentaires
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!