How to convert char data to double format?

19 vues (au cours des 30 derniers jours)
Y Choi
Y Choi le 20 Juil 2023
Déplacé(e) : Stephen23 le 21 Juil 2023
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
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']
val = 2×64 char array
'11 444 214141111 11111111111114111111111 1111111113111111111 111' '11 444 214141113 11111311111114111111121 1111111123111111111 111'
num = double(val)
num = 2×64
49 49 32 52 52 52 32 50 49 52 49 52 49 49 49 49 32 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 32 52 52 52 32 50 49 52 49 52 49 49 49 51 32 49 49 49 49 49 51 49 49 49 49 49 49 49

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
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))
result_double = 2×6
1.0e+00 * 11 444 214141111 1.11111111111141e+22 1.11111111311111e+18 111 11 444 214141113 1.11113111111141e+22 1.11111112311111e+18 111
%or
result_u64 = cell2mat(cellfun(@(S) sscanf(S, '%ld').', cellstr(val), 'uniform', 0))
result_u64 = 2×6
11 444 214141111 9223372036854775807 1111111113111111111 111 11 444 214141113 9223372036854775807 1111111123111111111 111
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) + "]")
result_sym = 

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!

Translated by