Effacer les filtres
Effacer les filtres

How can I separate complex data from a .txt file?

2 vues (au cours des 30 derniers jours)
Bou
Bou le 22 Nov 2013
Commenté : Bou le 25 Nov 2013
Hello, does anyone know how to separate the data below in 6 columns? The data consists of IV-measurements of a solar cell at 166 different time units. It can be divided in 6 parts which are separated by "-marks (part 2,3 and 4 are put together between"-marks).
1;date (between "-marks)
2,3 and 4;id,temperature,irradiance(between next "-marks)
5;current measurements(between next "-marks, very large data row)
6;voltage measurements(between next "-marks, very large data row)
Thanks in advance!
"2013-11-13 08:12:33",1239169,3.9675,28.4041,"1.115000;1.117000;1.116000;1.118000;1.117000;1.117000;1.116000;1.116000;1.116000;1.120000;1.117000;1.115000;1.117000;1.117000;1.117000....etc

Réponse acceptée

Simon
Simon le 22 Nov 2013
Hi!
It seems all your 6 fields in a row are separated with ",", right? The easiest is to read in the whole file and process each line. I used the sample line (all in one line!)
str = '"2013-11-13 08:12:33",1239169,3.9675,28.4041,"1.115000;1.117000;1.116000","1.118000;1.117000;1.117000"';
The code is
% positions of ','
ind = strfind(str, ',');
% start of each column
indstart = [1 ind+1];
% end of each column
indend = [ind-1 length(str)];
% get all columns
DateString = str(indstart(1):indend(1));
idString = str(indstart(2):indend(2));
temperatureString = str(indstart(3):indend(3));
irradianceString = str(indstart(4):indend(4));
currentString = str(indstart(5):indend(5));
voltageString = str(indstart(6):indend(6));
% convert current and voltage to numeric array
currentString = regexprep(currentString, '"', '');
currentArray = str2num(currentString);
voltageString = regexprep(voltageString, '"', '');
voltageArray = str2num(voltageString);
This can of course be done for multiple rows in the file and in vectorised form. It is just to get you started.
  3 commentaires
Simon
Simon le 22 Nov 2013
Like I did, except that you with "strfind" search for ';'. But the voltage column is already split in my example. What do you want to do with it?
Bou
Bou le 25 Nov 2013
I figured it out. Thanks a lot!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB 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!

Translated by