Reading a csv file with " " separators

5 ビュー (過去 30 日間)
kostas asamidis
kostas asamidis 2017 年 12 月 11 日
回答済み: Stephen23 2017 年 12 月 12 日
Hi ,
I have a big dataset(timeseries) in a csv file. It has the form bellow:
"step","data","prediction-1step","prediction-5step"
"int","float","float","float"
"","","",""
"1","21.2","0","0"
"2","16.4","0","0"
"3","4.7","0","0"
..................
..................
"15022","25.7","3.23","21.3"
I want to read the dataset in a way that i can access it by (Lets say i assign it in variable: Variable), Variable.step(i), Variable.data(i). Is that possible?I am new in Matlab and i cant find a way doing this. Thanks in advance.
  1 件のコメント
Stephen23
Stephen23 2017 年 12 月 12 日
編集済み: Stephen23 2017 年 12 月 12 日
Ugh. Who writes files like that? Why did they have to put quotation marks around numeric values?

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

回答 (1 件)

Stephen23
Stephen23 2017 年 12 月 12 日
opt = {'Delimiter',',"', 'MultipleDelimsAsOne',true, 'HeaderLines',3};
[fid,msg] = fopen('test.csv','rt');
assert(fid>=3,msg)
C = textscan(fid,'%f%f%f%f',opt{:});
fclose(fid);
M = cell2mat(C)
Giving:
M =
1 21.2 0 0
2 16.4 0 0
3 4.7 0 0
15022 25.7 3.23 21.3
The test file is attached (you did not provide a test file, so I had to create my own).

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by