Extract text from CSV file without knowing structure

조회 수: 4 (최근 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.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by