Filter löschen
Filter löschen

Importing data from *.asc file(s)

133 Ansichten (letzte 30 Tage)
NDKA
NDKA am 16 Apr. 2013
Beantwortet: Mike Scannell am 14 Feb. 2023
Hi,
In my day to day work, I log several data using CANalyzer to collect vehicle CAN network data and its a *.asc format. This log file contains all the CAN messages present on the network. But, I'd like to import the whole log file in to Matlab but would like to extract only specific rows. The rows can be identified by specific CAN ID.
For example, if the entire log file has a total of 1000 rows, it could be shared between two CAN IDs as 500 rows with CAN ID '222', and the rest of 500 rows with CAN ID '333'. What I'd like to achieve is, 'To load the entire log file but extract only the rows that contains CAN ID of '222'.
So, could some help me with any script file that would help me to solve this issue please.
Thanks
  4 Kommentare
Jan
Jan am 17 Apr. 2013
Bearbeitet: Jan am 17 Apr. 2013
I cannot guess, what the CAN ID is in this text file. Perhaps it is the 2nd number with the integer value, perhaps you mean the last entry "ID = ..." in each line. It is not an efficient idea to let us guess.
Posting the text snippet as text would be much more useful, because we could experiment with the data. Posting the new information inside the original question would be a good method also, because this is the place where reader need the information.
Please explain, if the first 5 lines can be skipped in every case, or how the header can be identified securely.
NDKA
NDKA am 17 Apr. 2013
Bearbeitet: Walter Roberson am 17 Apr. 2013
Sorry that I was not very clear in my explanation.
Yes, we can skip the header in each file (in fact the first 7 rows).
CAN ID I was talking about is in the 3rd column (1F3, 1E5, 710 etc).
I tried to copy and paste the data, but it looks very confusing, so I posted as picture. I didn't realize the posting the data itself would be more helpful. Here it is...
date Thu Apr 11 04:41:25 pm 2013
base hex timestamps absolute
internal events logged
// version 8.0.0
Begin Triggerblock Thu Apr 11 04:41:25 pm 2013
0.000000 Start of measurement
0.001316 CAN 1 Status:chip status error active
0.001399 1 1F3 Rx d 3 00 10 00 Length = 146000 BitCount = 77 ID = 499
0.002763 1 1E5 Rx d 8 4C 00 21 10 00 00 00 B9 Length = 228000 BitCount = 118 ID = 485
0.003009 1 710 Rx d 8 00 5F 00 00 00 00 13 BE Length = 238000 BitCount = 123 ID = 1808
0.003175 1 C7 Rx d 4 00 38 26 9B Length = 158000 BitCount = 83 ID = 199
0.003349 1 1CC Rx d 4 00 00 00 00 Length = 165883 BitCount = 87 ID = 460
0.003586 1 F9 Rx d 8 00 DA 40 33 D0 63 FF 1C Length = 228000 BitCount = 118 ID = 249
0.003738 1 1CF Rx d 3 00 00 05 Length = 144000 BitCount = 76 ID = 463
0.003976 1 711 Rx d 8 00 23 00 7E FF EB FC 6F Length = 230000 BitCount = 119 ID = 1809
0.004148 1 1D0 Rx d 4 00 00 00 00 Length = 164000 BitCount = 86 ID = 464
0.004382 1 C1 Rx d 8 30 14 F6 08 32 B4 F7 70 Length = 226000 BitCount = 117 ID = 193
0.004615 1 C5 Rx d 8 31 27 F8 44 32 B0 F8 5C Length = 224121 BitCount = 116 ID = 197
0.004825 1 BE Rx d 6 00 00 4D 00 00 00 Length = 202242 BitCount = 105 ID = 190
0.005051 1 D1 Rx d 7 80 00 BF FE 00 FE 00 Length = 218121 BitCount = 113 ID = 209
0.005292 1 C9 Rx d 8 80 2C 5A 60 00 00 18 00 Length = 232242 BitCount = 120 ID = 201
0.005538 1 1C8 Rx d 8 80 00 00 00 FF FE 3F FE Length = 238121 BitCount = 123 ID = 456
0.005774 1 18E Rx d 8 00 00 00 84 78 46 08 45 Length = 228242 BitCount = 118 ID = 398

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 17 Apr. 2013
CANID = '1CF'
fid = fopen('YourFile.asc', 'rt');
for K = 1 : 7; fgetl(fid); end %skip header
CANpattern = ['^\S+\s+\d+\s+' CANID];
while true
thisline = fgetl(fid);
if ~ischar(thisline); break; end %end of file
if ~regexpi(thisline, CANpattern); next; end %line is wrong ID
... do something with thisthis line
end
fclose(fid);
  2 Kommentare
NDKA
NDKA am 18 Apr. 2013
Bearbeitet: Walter Roberson am 18 Apr. 2013
Thank you very much Walter. I'm having hard time to extract the data of that particular row. For example, if the total file has 5 occurrence of CAN ID's '1CF', I would like to extra the column of each row in to an array. I tried to use 'textscan' for that, but its not working...
CANID = '1CF'
fid = fopen('YourFile.asc', 'rt');
for K = 1 : 7; fgetl(fid); end %skip header
CANpattern = ['^\S+\s+\d+\s+' CANID];
while true
thisline = fgetl(fid);
if ~ischar(thisline); break; end %end of file
if ~regexpi(thisline, CANpattern); next; end %line is wrong ID
C = textscan(fid, '%f%f%s%s%s%f%s%s%s%s%s%f%s%s%f%s%s%f');
end
fclose(fid);
0.003738 1 1CF Rx d 3 00 10 05 Length = 144000 BitCount = 76 ID = 463
0.003738 1 1CF Rx d 3 01 20 05 Length = 144000 BitCount = 76 ID = 463
0.003738 1 1CF Rx d 3 02 30 05 Length = 144000 BitCount = 76 ID = 463
0.003738 1 1CF Rx d 3 03 40 05 Length = 144000 BitCount = 76 ID = 463
0.003738 1 1CF Rx d 3 04 50 05 Length = 144000 BitCount = 76 ID = 463
Walter Roberson
Walter Roberson am 18 Apr. 2013
While I was taking a shower I realized that I had a minor bug in the pattern. It should be
CANpattern = ['^\S+\s+\d+\s+' CANID '\s'];
The extra \s allows the IDs 1 and 1C to be distinguished from 1CF .
Your loop is overwriting C each time. I suggest,
inlines = {};
while true
thisline = fgetl(fid);
if ~ischar(thisline); break; end %end of file
if ~regexpi(thisline, CANpattern); next; end %line is wrong ID
inlines{end+1} = thisline;
end
fclose(fid)
inlines - char(inlines); %textscan cannot deal with cellstr
fieldscell = textscan(inlines.', '%f%f%s%s%s%f%s%s%s%s%s%f%s%s%f%s%s%f');
disp(fields{1}) %first column
Using the .' on the character array is a trick... don't expect it to make perfect sense!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Mike Scannell
Mike Scannell am 14 Feb. 2023
This is an old question, but still comes up when searching about using Matlab to read CAN .asc files.
Since 2017, the Vehicle Network Toolbox has the function canSignalImport which can read this type of file and provides the ability to import only selected messages.

Kategorien

Mehr zu Data Import and Export finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by