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.

采纳的回答

Chandra Kurniawan
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
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
Walter Roberson 2011-11-30
Do your columns have fixed width, or do they have a delimiter between the fields?

charles atlas
charles atlas 2011-12-1
They have spaces between them. they are numeric and text data

charles atlas
charles atlas 2011-12-1
In other words the data that I read in with fopen and textscan ends up looking like this:
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 real dtat has 10 columns of numeric and text data like that but the first 5 columns basically look like that. When I say:
X{4}=
'heating'
'heating'
'heating'
IT displays it in that way and the code doesnt read 'cooling down' because it thinks 'down' is another column and the code is not programmed to read 11 columns, only 10.
  2 个评论
Walter Roberson
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.
charles atlas
charles atlas 2011-12-1
Thank you,
allthough long, the code Chandra Kurniawan gave works fine.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by