Is there a faster way than str2double() to convert from a string array into a matrix containing doubles?
Mostrar comentarios más antiguos
Hi, i am working with large .txt files, that I imported as a string array. A big part of this .txt file contains numeric values, that I want to convert to doubles. Since the array is sufficiently large (500.000 x 25), it takes MATLAB very long to convert these strings into doubles using str2double(). Is there a faster way to convert a String array into a numeric matrix?
8 comentarios
Fangjun Jiang
el 30 de Nov. de 2017
For your case, would it be easier to get the numerical data directly from the text file? Did you try importdata()?
Greg
el 30 de Nov. de 2017
Share the code you've actually tried with us; or at least a reasonable mock-up of it. As Fangjun mentioned, it might not be str2double that's your problem. We can't know without seeing how you imported the file and how you are trying to convert them.
KSSV
el 1 de Dic. de 2017
You need not to import the file as a string........you can import it as a doubles...code depends on how the file is....attach your txt file.
Stephen23
el 1 de Dic. de 2017
The faster way would be to import the numeric data as numeric data. Upload your file by clicking the paperclip button.
Bjorn Sauren
el 1 de Dic. de 2017
@Bjorn Sauren: MATLAB has more file importing functions than just importdata, and plenty on volunteers here who have experience using them. As you have been asked before, please upload the data file by clicking the paperclip button.
per isakson
el 1 de Dic. de 2017
- "mport tool, it works fine.. I don't understand why" you didn't give the gui a helping hand?
- (500000*25)*8/1e6 makes 100MB, which shouldn't be a problem
- See Import Large Text File Data in Blocks
Bjorn Sauren
el 1 de Dic. de 2017
Respuesta aceptada
Más respuestas (1)
Renwen Lin
el 3 de Mzo. de 2019
Editada: per isakson
el 5 de Mzo. de 2019
1 voto
Try this!
3 comentarios
PL.R
el 16 de Feb. de 2021
Thank you. This should be the accepted answer since the OP asks for a way of converting faster string to double and not how he could make it's file reading faster.
Jan
el 17 de Feb. de 2021
You are right. The OP had speed problems and thought that a faster STR2DOUBLE solves the problem. But avoiding the need to call STRDOUBLE is even faster.
The FEX submission suffers from some severe conversion problems:
str2doubleq('Inf') % NaN instead of Inf
str2doubleq('.i5') % 5 instead of NaN
str2doubleq('i') % 0 instead of 0 + 1i
str2doubleq('1e1.4') % 0.4 instead of NaN
str2doubleq('--1') % -1 instead of NaN
s = '12345678901234567890';
str2doubleq(s) - str2double(s) % 2048
s = '123.123e40';
str2doubleq(s) - str2double(s) % 1.547e26
str2double('2.236')-str2doubleq('2.236') % is not 0 ('2.235' is fine)
str2double('1,1')-str2doubleq('1,1') % 9,9 instead 0
isreal(str2doubleq('1')) % 0 instead of 1
str2double('2.236')-str2doubleq('2.236') % is not 0 ('2.235' is fine)
str2double('1,1')-str2doubleq('1,1') % 9,9 instead 0
A part of the speed up is based on a missing memory cleanup. This function leaks memory, because it allocates strings by mxArrayToString without free'ing it. With large cells this exhausts GB of RAM in seconds and you habve to restart Matlab to free it.
This tool is fast, but not reliably enough for scientific or productive work.
Walter Roberson
el 17 de Feb. de 2021
I would have expected nan for the 1,1 case?
Categorías
Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!