Extract text from CSV file without knowing structure

4 visualizaciones (últimos 30 días)
Panda_friend
Panda_friend el 9 de Feb. de 2015
Comentada: Panda_friend el 10 de Feb. de 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!

Respuesta aceptada

Luuk van Oosten
Luuk van Oosten el 10 de Feb. de 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.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by