reading columns from a text file with errors
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
Chandra Kurniawan
am 1 Dez. 2011
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 Kommentar
Chandra Kurniawan
am 1 Dez. 2011
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
Weitere Antworten (3)
Walter Roberson
am 30 Nov. 2011
Do your columns have fixed width, or do they have a delimiter between the fields?
0 Kommentare
charles atlas
am 1 Dez. 2011
2 Kommentare
Walter Roberson
am 1 Dez. 2011
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.
Siehe auch
Kategorien
Mehr zu Data Import and Export finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!