Problem by using str2double with csv file
Show older comments
i have some csv files and i extract some rows from them using readtable (i tried also readmatrix). when i used str2double to the extracted vectors, it gives wrong result ( with e+16 and e+5). Have please anyone help me ?
6 Comments
Adam Danz
on 24 Jun 2021
How is " e+16 and e+5" a wrong result? What result were you expecting?
Try to provide all of the relevant information so we can fully undestand the problem.
Ahmed Ghouma
on 24 Jun 2021
Scott MacKenzie
on 24 Jun 2021
It might help if you post the data file.
Ahmed Ghouma
on 24 Jun 2021
Scott MacKenzie
on 24 Jun 2021
Well, that file might have csv as the suffix but it does not contain comma-separated values. There are no commas in the file.
Ahmed Ghouma
on 24 Jun 2021
Edited: Ahmed Ghouma
on 24 Jun 2021
Answers (1)
the cyclist
on 24 Jun 2021
Edited: the cyclist
on 25 Jun 2021
The issue is that your input file uses commas as the decimal separator, rather than a period ("decimal point"). This means that MATLAB is treating those elements a text rather than numbers.
I did a global replacement of commas for periods, and then your file loads the data as numbers.
T15 = readtable('73_4.csv');
output = T15(1:3,2)
I don't think there is a way to change MATLAB's requirement of using a decimal point for input. So, I think that doing the replacement in the input file is your only option. (But I could be wrong.)
16 Comments
Ahmed Ghouma
on 25 Jun 2021
the cyclist
on 25 Jun 2021
@Stephen Cobeldick, thanks for pointing that out! I actually searched this forum and elsewhere before making my statement, but I still thought there was a high probability that someone would swoop in and correct that!
@Ahmed Ghouma, according to this comment, it is a "fairly recent" change to readtable that allows specifying the decimal separator. That was written in August 2020, so it is possible it is not in R2019b. I have R2020a on a machine, and it works there.
Ahmed Ghouma
on 25 Jun 2021
Ahmed Ghouma
on 26 Jun 2021
Stephen23
on 26 Jun 2021
"Could you please suggest a solution ?"
Umm... the obvious solution is to stop using STR2DOUBLE on numeric data.
Why are you trying to use STR2DOUBLE to convert numeric data into numeric data?
Assuming you are using the original file, with commas as separators (and not the file I posted, where I converted those to decimal points), then the syntax that @Stephen Cobeldick posted will read everything in as numeric. You don't need to convert to double anymore, because there should be strings. You can convert from the table to a numeric array if you need to:
% Read from the original file you posted. (You should keep reading from your local file.)
T = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/664395/73_4.csv', 'Delimiter',';', 'DecimalSeparator',',');
c = table2array(T(:,1))
Ahmed Ghouma
on 26 Jun 2021
Edited: Ahmed Ghouma
on 26 Jun 2021
As I said, the syntax is for the file that has commas. You are obviously using it on the file that uses periods. (It's easy to get confused. I used the wrong file in my last post.)
USE YOUR LOCAL FILE THAT HAS COMMAS AND NOT PERIODS.
% Read from the original file you posted. (You should keep reading from your local file.)
T = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/664360/73_4.csv', 'Delimiter',';', 'DecimalSeparator',',');
T(1:3,:)
If you do use the table that has been converted, then you do not need to specify the decimal separator:
T = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/664395/73_4.csv','Delimiter',';');
T(1:3,:)
Ahmed Ghouma
on 26 Jun 2021
Ahmed Ghouma
on 26 Jun 2021
In this code, I am using the original file you posted. It has commas, and the code gives a double when converted from a table. I don't understand what you are doing. But if you look at the screenshot you posted above, you are clearly pulling from a file that uses the decimal point.
% Read from the original file you posted. (You should keep reading from your local file.)
T = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/664360/73_4.csv', 'Delimiter',';', 'DecimalSeparator',',');
c = table2array(T(:,2))
class(c)
Ahmed Ghouma
on 26 Jun 2021
Ahmed Ghouma
on 26 Jun 2021
Edited: Ahmed Ghouma
on 26 Jun 2021
"i think in some files it s used commas as the decimal separator, rather than a period ("decimal point") and in some others a decimal point."
I cannot imagine any application or tool randomly alternating between decimal commas and decimal points.
What is more likely is that someone is opening and saving some of the files using Excel, which completely obliterates the original file format. If you want reliable, robust file parsing, avoid saving with MS Excel.
For looking at any text file, including CSV/TSV/SCSV files you should be using a reliable text editor, e.g. notepad++.
In the unlikely event that you really are using an application that really does create files with random decimal separators, you should first try calling
with the delimiter option set to ';' and see if it can correctly identify the decimal separator character.
For example you could simply call delimitedTextImportOptions twice (once with dot, once with comma) and check which one has the desired variable class (double).
If that does not work, you can simply read in a line or two of the file as text and perform your own simple detection algorithm, it would only take a few lines of code.
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!