readmatrix with numbers and text from .csv file

116 ビュー (過去 30 日間)
Mike Mierlo van
Mike Mierlo van 2020 年 1 月 12 日
回答済み: Meg Noah 2020 年 1 月 12 日
Hi guys,
I have a very large .csv file to read which contains text and numbers. To explain I have a very simplified .csv file below called test.csv.
test.csv =
1,2,3,test
a,b,5,6
7,asdf,8,9,d
As can be seen this is a 3x4 matrix of numbers and text. I want this read by MATLAB and put into a matrix.
When I try readmatrix('test.csv') I get the numbers but instead of text I get 'NaN'. I want to have the numbers and the text into the matrix.
Please help me.

採用された回答

Meg Noah
Meg Noah 2020 年 1 月 12 日
The problem is your last line has 5 columns. This seems to do what you're asking, and hopefully will be enough to get you started:
opts = detectImportOptions('simplified.csv');
opts.ExtraColumnsRule = 'ignore';
disp(opts)
opts = setvartype(opts,{'Var1','Var2','Var3',},{'categorical','categorical','categorical'});
x = readtable('simplified.csv',opts);
y = table2cell(x);
It would help to have your file, and a better understanding of what you intend to do with the matrix.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by