Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
Bou
Bou am 22 Nov. 2013
Kommentiert: Bou am 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

Akzeptierte Antwort

Simon
Simon am 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 Kommentare
Simon
Simon am 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 am 25 Nov. 2013
I figured it out. Thanks a lot!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by