Fast String to Double Conversion (Scripts) Publisher's description
from Lauri Tamminen
str2doubleq is equivalent to the Matlab built-in str2double function that converts char or cellstr array to appropriate double arrays
str2doubleq is equivalent to the Matlab built-in str2double function that converts char or cellstr array to appropriate double arrays. The drawback of built-in str2double is that it becomes very slow when the dataset becomes larger.
str2doubleq exploits the mex-gateway to use c++ fast string handling capabilities and the std::stringstream properties. The conversion uses same ideas that is used in boost::lexical_cast
Function has been tried to program exactly the same as str2double. However it does not currently support complex numbers. This means that it returns NaN values for inputs of type 'd+[+-]id+'
Original demand for the function arised from certain market data parsing problems that had to be done in real time. Now Matlab can be as fast as traditional programming languages in these types of string parsing problems.
*Copy the file str2doubleq.cpp somewhere in hard drive. (Example C:Teststr2doubleq.cpp)
*Launch Matlab and compile the source file to generate machine dependent binary. If you have not selected a compiler this needs to be done first (run mex -setup in command window).
* Source is compiled typing mex <c-source folder>
(Example mex C:Teststr2doubleq.cpp)
*Place the generated str2doubleq.mexw32 (32-bit) or str2doubleq.mexw64 (64-bit) to Matlab's scope (set path- folder group)
Now you can use the function in normal matlab fashion. Run the testcases script TestPerformance (included in zip-file)
* Segmentation violation bug fixed.
* Fixed a bug when empty array was passed. Now str2doubleq returns NaN instead of empty array. (Consistent with str2double)
*Tweaked some extra performance by defining istringstream to be static variable inside the string_to_double helper function. This prevents the overhead that is being built in istringstream constructor in subsequent calls to function.
*Made a note in the code about atof usage. Atof is propably the most optimal way to make the conversion. However its behaviour is not the same as built-in str2double (see the notes in source)
System Requirements:MATLAB 7.11 (2010b)
Program Release Status: New Release
Program Install Support: Install and Uninstall