Problem by using str2double with csv file

조회 수: 7 (최근 30일)
Ahmed Ghouma
Ahmed Ghouma 2021년 6월 24일
편집: Stephen23 2021년 6월 26일
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
Scott MacKenzie
Scott MacKenzie 2021년 6월 24일
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
Ahmed Ghouma 2021년 6월 24일
편집: Ahmed Ghouma 2021년 6월 24일
semicolon is the delimiter ";" and what should be done ?
when i use xlsread it gives NaN

댓글을 달려면 로그인하십시오.

답변 (1개)

the cyclist
the cyclist 2021년 6월 24일
편집: the cyclist 2021년 6월 25일
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');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
output = T15(1:3,2)
output = 3×1 table
Trace1_Gain_Real ________________ -0.00011834 -0.00012968 -0.00017933
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
Ahmed Ghouma
Ahmed Ghouma 2021년 6월 26일
편집: Ahmed Ghouma 2021년 6월 26일
@Stephen Cobeldick @the cyclist could you please try it with this file
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. So have you please an idea how to specify if it is a decimal point used so i can use this
T = readtable('73_4.csv', 'Delimiter',';')
and if a comma than
T = readtable('73_4.csv', 'Delimiter',';', 'DecimalSeparator',',')
i m very thankful
Stephen23
Stephen23 2021년 6월 26일
편집: Stephen23 2021년 6월 26일
"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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by