Extract text from CSV file without knowing structure

2 ビュー (過去 30 日間)
Panda_friend
Panda_friend 2015 年 2 月 9 日
コメント済み: Panda_friend 2015 年 2 月 10 日
Hey guys,
Here's my problem :
I have a csv file in which the rows contain sentences of varying number of elements.
I have seen that in order to use "textscan", you need to know the formatSpec, eg "%s%s%s%s", which I don't think is possible in my case.
What could I do to solve this problem?
Thanks!

採用された回答

Luuk van Oosten
Luuk van Oosten 2015 年 2 月 10 日
Hi
It depends if your csv file is comma/tab/semicolon delimited. for now i'll assume you have something that looks like an EXCEL file which you saved as .csv. In column A you have several rows (say 1:4) with sentences such as:
this is the first sentence
this is the second sentence but it has another length then the first one
this is the third
hey that sentence was different
Now, define your data and he delimiter (here it is a tab)
filename = 'yourdata.csv';
delimiter = '\t';
formatSpec = '%s%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'ReturnOnError', false);
fclose(fileID);
Create output variable
test = table(dataArray{1:end-1}, 'VariableNames', {'sentenceone'});
Note: this is just copy-paste from the automated generated script, which you can use to import .csv files. Stuff changes of course if -for instance- your .csv file is not tab delimited.
  1 件のコメント
Panda_friend
Panda_friend 2015 年 2 月 10 日
Perfect, it works well with my document! Thanks

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by