reading columns from a text file with errors
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a code displayed below
clear
clc
file1=input('enter file name in single quotes ');
fid = fopen(file1,'rt');
X = textscan(fid,'%s%s%f%s%s%s%f%f%f%f%s%s%s%s',...
'HeaderLines',7);
fclose(fid);
time= X{1}; status = X{6}; length=X{10};
This reads data i need from a file so that I can analyze it. The problem wiht this code is that sometimes the 4th column of data has one word of text and sometimes it has two.
the 4th column would look like this:
X{4}=
'heating'
'heating'
'heating'
'heating'
'cooling down'
etc.
When matlab reads the file it gets to the row that says 'cooling down' in the 4th column and thinks that 'down' is another column. textscan stops working at that point and I only get the data up to the point where the row that says 'cooling down' appears.
Is there any way to merge the words cooling down to say 'coolingdown' so that the code will read all of the data? Or is there another alternative? I need to read all of the data from this file. I do not care what happens to the 4th column, because I am not interested in the data from it but I need the other columns of data.
댓글 수: 0
채택된 답변
Chandra Kurniawan
2011년 12월 1일
Hello, I think it is realy difficult to read 'cooling down' as 'cooling_down' or 'coolingdown' using textscan. But I found new way to do that.
First you need to edit every word 'cooling down' in your text file to be 'cooling_down'
And then you can open and read the text file normally with your script.
This code bellow can be used to replace all 'cooling down' word to be 'cooling_down'
I have a text file named 'data01.txt' that contains :
time 123 0.00 heating 5:23.16 etc
time 321 0.02 heating 5:23.18 etc
time 435 0.07 cooling down 5:23.20 etc
The code:
clear; clc;
file01 = input('enter file name : ','s');
fid01 = fopen(file01, 'rt');
tline = fgetl(fid01);
x = 1;
while ischar(tline)
a{x} = tline;
b{x} = regexprep(a{x}, 'cooling down', 'cooling_down');
tline = fgetl(fid01);
x = x + 1;
end
fclose(fid01);
file02 = 'data02.txt';
fid02 = fopen(file02, 'w');
for x = 1 : length(b)
fprintf(fid02, '%s\n', b{x});
end
fclose(fid02);
---------------------------------
Just enter data01.txt for file01 above. And then you will get 'data02.txt' that contains :
time 123 0.00 heating 5:23.16 etc
time 321 0.02 heating 5:23.18 etc
time 435 0.07 cooling_down 5:23.20 etc
Then you can use your code to read the text file normally
clear; clc;
file = input('enter file name in single quotes : ','s');
fid = fopen(file,'rt');
X = textscan(fid,'%s %f %f %s %s %s');
fclose(fid);
---------------------------------
Type X{4} in the command window and you'll get
>> X{4}
ans =
'heating'
'heating'
'cooling_down'
>>
Hope this will help you.
댓글 수: 1
Chandra Kurniawan
2011년 12월 1일
Now, just edit 'data01.txt' to be :
time 123 0.00 heating 5:23.16 etc
time 321 0.02 heating 5:23.18 etc
time 435 0.07 cooling down 5:23.20 etc
time 123 0.00 heating 5:23.16 etc
time 321 0.02 heating 5:23.18 etc
time 435 0.07 cooling down 5:23.20 etc
time 435 0.07 cooling down 5:23.20 etc
time 123 0.00 heating 5:23.16 etc
And you'll get 'data02.txt' as :
time 123 0.00 heating 5:23.16 etc
time 321 0.02 heating 5:23.18 etc
time 435 0.07 cooling_down 5:23.20 etc
time 123 0.00 heating 5:23.16 etc
time 321 0.02 heating 5:23.18 etc
time 435 0.07 cooling_down 5:23.20 etc
time 435 0.07 cooling_down 5:23.20 etc
time 123 0.00 heating 5:23.16 etc
추가 답변 (3개)
Walter Roberson
2011년 11월 30일
Do your columns have fixed width, or do they have a delimiter between the fields?
댓글 수: 0
charles atlas
2011년 12월 1일
댓글 수: 2
Walter Roberson
2011년 12월 1일
That *is* fixed-width columns. That makes the task easier in some ways.
Unfortunately I have an appointment to go to now and cannot show appropriate code at this time.
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import and Export에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!