Extract text from CSV file without knowing structure
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Panda_friend
le 9 Fév 2015
Commenté : Panda_friend
le 10 Fév 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 commentaires
Réponse acceptée
Luuk van Oosten
le 10 Fév 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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Text Files dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!