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!