How to force readtable to read only doubles?

조회 수: 63 (최근 30일)
G. P.C.
G. P.C. 2015년 12월 3일
댓글: Walter Roberson 2019년 11월 10일
I am currently trying to import an excel file in Matlab via readtable.
The excel file I am working with has the following struncture:
Col1
1
2
n.d
4
3,5
As the data is not the same along the cells readtable correclty builds a table in the worksapce with the correct variable name (Col1) but the values are all read as strings, resulting in something like this:
Col1
------
'1'
'2'
'n.d'
'4'
'3,5'
As I would like to handle only the numbers (and thus it is ok for me to discard the text such as n.d) how can I specify readtable to read everything as double resulting in a table in the workspace as follows?
Col1
------
1
2
NaN
4
3.5
Of course a solution could be to use varfun and cast all the data, but is it possbile perform this task by using just readtable?

답변 (2개)

Walter Roberson
Walter Roberson 2015년 12월 3일
I think you will need to do it in post-processing. Extract the strings, regexprep of comma to ',', then str2double(), and write the results back to Col1. Then you can delete the rows that became NaN.
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 11월 10일
I do not know why I did not think of it before, but in the case where you know all of the possible text strings, you can handle this without the above effort, just by using the TreatAsEmpty option. This would require that the marker always be 'n.d' or any specific list you give, such as {'n.d', 'n.d.', 'nd', 'n/a'} .

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


useruser
useruser 2019년 11월 5일
table = readtable('file.csv');
table.Col1 = str2double(table.Col1);
converts everything but numbers to MATLAB's NaN.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by