Use Readtable() on a single-column CSV but generated multiple variable names

2 ビュー (過去 30 日間)
Zzz
Zzz 2021 年 6 月 16 日
コメント済み: Asvin Kumar 2021 年 6 月 23 日
When using readtable() to read a single-column CSV file, the function reads more than one variable name.
% Read CSV data
dataset = readtable(CSVpath, "TextType","string", "ReadVariableNames", true)
vNames = dataset.Properties.VariableNames
% Expected
vNames = 1x1 cell array {'Notes'}
% Received
vNames = 1x7 cell array {'Call_1__'} {'US___'} {'Var3'} {'Var4'} {'Var5'} {'Var6'} {'Var7'}
There were 2 warnings: one telling me to use 'preserve' to preserve variable names; the other says that my variable names are too long (truncated to the max length).
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.
How can I only read the one column I want?
  2 件のコメント
dpb
dpb 2021 年 6 月 16 日
Attach the file; we can't tell what's in it to confuse readtable without seeing it.
If it's really, really long, just save and attach the first 50 rows or something representative.
Asvin Kumar
Asvin Kumar 2021 年 6 月 23 日
Like dpb says, it's hard to suggest fixes without seeing the actual data. Here's a suggestion. You can mention the range of your variable names by creating custom file import options. You can set the VariableNamesRange property of a SpreadsheetImportOptions object.

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by