Hi,
I've tried to use the import data function for reading numeric data and text from the same CSV file. Each line of the header (first 10 lines) starts with a semicolon. The numeric data starts in row 11. That is represented in 4 columns. Sometimes there is an error message between the data rows starting with a semicolon. When I use the importdata function the data reading stops at the first error message line, though it is followed by useful data. Can anyone suggest me a way for reading in the whole file and accepting lines starting with semicolons?
THanks!

1 Comment

Fangjun Jiang
Fangjun Jiang on 7 Sep 2011
Hard to tell without an example and your expected outcome.

Sign in to comment.

 Accepted Answer

Walter Roberson
Walter Roberson on 7 Sep 2011

0 votes

textscan() with Headerlines set to 10, and with CommentStyle set to ';'

2 Comments

Laszlo Grand
Laszlo Grand on 7 Sep 2011
Sorry for my previous not detailed question. I am trying to be more specific now.
All I want to do is to delete those error lines in the data block that starts with a semicolon and overwrite the old CSV with the new data. The header must remain the same.
The header consists of 10 lines:
;Title http://www.gcdataconcepts.com X6-2mini
;Version 1148 Build num 0xF44 Build date 20110707 16:36:02 SN:CCDC22011001387
;Start_time 2011-08-18 13:27:28.144
;Switch Unknown
;Temperature 32 deg C Vbat 3937 mv
;Gain low
;SampleRate 40 Hz
;Deadband 0 counts
;DeadbandTimeout 0 sec
;Headers time Ax Ay Az
A snippet of the data section (432.000 rows, 4 columns after the header) with the error line looks like this:
1735.672 317 -10 -151
1735.694 316 -19 -154
1735.716 313 -38 -161
;1735.857504 smbus error 5
1735.88 351 -21 -169
1735.902 334 -17 -132
1735.924 288 -12 -71
Can you help me with the code that can do this?
Thanks!!!
Walter Roberson
Walter Roberson on 8 Sep 2011
filename = 'YourInput.csv';
outname = ['NEW_' filename];
infid = fopen(filename, 'rt');
outfid = fopen(outname, 'wt');
for K = 1:10
fwrite(outfid,fgets(infid));
end
while true
thisline = fgets(infid);
if ~ischar(thisline); break; end
if thisline(1) ~= ';'; fwrite(outfid, thisline); end
end
fclose(infid);
fclose(outfid);

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!