필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Bou
Bou 2013년 11월 22일
댓글: Bou 2013년 11월 25일
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

채택된 답변

Simon
Simon 2013년 11월 22일
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
Simon
Simon 2013년 11월 22일
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 2013년 11월 25일
I figured it out. Thanks a lot!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by