필터 지우기
필터 지우기

How to read column in excel table as a specific data type?

조회 수: 15 (최근 30일)
Zeyad Elreedy
Zeyad Elreedy 2023년 3월 13일
댓글: Zeyad Elreedy 2023년 3월 13일
Hello. I am trying to read an excel table that contains multiple columns. My understanding is that MATLAB looks at the first row of each column to determine its data type. For one of my columns, I have both numeric values and strings. Since the numeric values are first, this column returns the strings as NaN, as it has identified the whole column as type double. I would like to convert this column to cells, as I am planning to create a categorical array from them. However, I am unable to do so at the moment due to the NaN entries. Therefore, I am trying to find a way to load the column as a cell straight away, so the string entries are not lost as NaN.

채택된 답변

Walter Roberson
Walter Roberson 2023년 3월 13일
filename = 'appropriate file name goes here';
opts = detectImportOptions(filename);
opts = setvartype(opts, COLUMNNUMBER, 'categorical');
T = readtable(filename, opts);
  댓글 수: 2
Walter Roberson
Walter Roberson 2023년 3월 13일
If there are no headers then you might want
opts = detectImportOptions(filename, 'ReadVariableNames', false);
Zeyad Elreedy
Zeyad Elreedy 2023년 3월 13일
Works perfectly, thanks a lot!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by