Readtable Returns "NaN" to the string type value if one column contains both number type and string type values

조회 수: 28 (최근 30일)
Hi, I have a quesiton regarding Matlab readtable commend
For example, I created a table on csv file as below, which one column includes number-type value at the first row and the string-type value at the second row.
Then I use readtable to read this csv file. It returns "NaN" to all string type value, as below. Therefore, I lost the information of the string-type data.
Can someone help me to resove this issue so I can read both number-type data and string-type data? I am currently using Matlab2023a.
Thank you in advance!

채택된 답변

Stephen23
Stephen23 2023년 10월 30일
편집: Stephen23 2023년 10월 30일
Use READCELL:
C = readcell('test.csv')
C = 3×4 cell array
{'value0' } {'value1' } {'value2' } {'value3' } {[ 1]} {[ 2]} {[ 3]} {[ 4]} {'gfedcba'} {'gfedcba'} {'gfedcba'} {'gfedcba'}
Everything that is text is text, everything that is numeric is numeric. Easy.

추가 답변 (1개)

Dyuman Joshi
Dyuman Joshi 2023년 10월 30일
편집: Dyuman Joshi 2023년 10월 30일
Each variable/column of a table is expected to be a homogenous array. The first value read in each column is a numeric value, and the rest of the values are not. Thus they are treated as Not a Number.
You can either
> Read all data as strings
or
> Use readcell and (if you want to,) convert to table via cell2table

카테고리

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

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by