Extract text from CSV file without knowing structure
2 views (last 30 days)
Show older comments
Panda_friend
on 9 Feb 2015
Commented: Panda_friend
on 10 Feb 2015
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!
0 Comments
Accepted Answer
Luuk van Oosten
on 10 Feb 2015
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.
More Answers (0)
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!