How to read numerics as strings with readtable?

조회 수: 277 (최근 30일)
Leon
Leon 2020년 3월 4일
댓글: Al in St. Louis 2021년 4월 21일
I have a table in spreadsheet (xlsx). One of the column is composed of either a numerical number, or several of them, like the below:
2
3
5,7
9
7
4
2,8,9
5
When I use readtable:
T1 = readtable ('Observations.xlsx');
the results are like this:
2
3
NaN
9
7
4
NaN
5
My question is how do I read the info as a string, so that the groups like '5,7', and '2,8,9' are all captured?
I tried:
T1 = readtable ('Observations.xlsx', 'Format','%s %s %s');
But got the below error:
Invalid value for "Format". Must be "auto" for spreadsheet files.
  댓글 수: 1
Ruger28
Ruger28 2020년 3월 4일
편집: Ruger28 2020년 3월 4일
Strange, I copied and pasted your values into excel, used readtable, and got this:
T =
7×1 table
x2
_______
'3'
'5,7'
'9'
'7'
'4'
'2,8,9'
'5'
Can you share you .xlsx file? I am assuming it is a number format issue.

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

채택된 답변

Guillaume
Guillaume 2020년 3월 4일
opts = detectImportOptions(yourfile);
opts = setvartype(opts, whichevervariable, 'string'); %or 'char' if you prefer
data = readtable(yourfile, opts)
Where whichevervariable is a char vector, cell array of char vector, string array containing the name(s) of the variable(s) that must be read as string/char or a numeric vector of variable indices.
  댓글 수: 2
Leon
Leon 2020년 3월 4일
Works well. Exactly what I need!
Thank you so much!
Al in St. Louis
Al in St. Louis 2021년 4월 21일
Given that Mathworks has broken readtable, it's good that they have provided this workaround so that we can keep using our data.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by